我需要创建一个用于监视的API,因此需要响应将是纯文本,而不是json或xml
我所做的是:
检查了我的应用程序Yaml是否包含文本:
types:
all: '*/*'
atom: application/atom+xml
css: text/css
csv: text/csv
excel: application/vnd.ms-excel
xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
form: application/x-www-form-urlencoded
html:
- text/html
- application/xhtml+xml
js: text/javascript
json:
- application/json
- text/json
multipartForm: multipart/form-data
pdf: application/pdf
rss: application/rss+xml
text: text/plain
hal:
- application/hal+json
- application/hal+xml
xml:
- text/xml
- application/xml
添加到我的控制器中:static responseFormats = ['all']
,也尝试过static responseFormats = ['text']
以我的方法做:
respond mystring, formats:['text']
但是仍然尝试将其转换为JSON并抛出http 406错误的方法仍然很麻烦
如何配置控制器以使用纯文本格式
谢谢
class MonitorApi4Controller {
static responseFormats = ['text']
static allowedMethods = [ monitorScrape: "GET"]
MonitorService getMonitorService(){
return ApplicationContextHolder.getBean('monitorService')
}
def monitorScrape(){
def message = getMonitorService().serviceMethod()
render message
}
}