![](/img/trans.png)
[英]how to respond/send multiple objects from controller to view in grails?
[英]how to set the controller to respond as plain text in Grails
我需要创建一个用于监控的 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']
但仍然 grails 试图将其转换为 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.