如何将控制器设置为在Grails中以纯文本格式响应

问题描述 投票:0回答:1

我需要创建一个用于监视的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错误的方法仍然很麻烦

如何配置控制器以使用纯文本格式

谢谢

grails groovy grails-controller
1个回答
0
投票
我知道该怎么做我在解决问题的方法中使用了render而不是response,但仍然不确定为什么使用response无法实现

class MonitorApi4Controller { static responseFormats = ['text'] static allowedMethods = [ monitorScrape: "GET"] MonitorService getMonitorService(){ return ApplicationContextHolder.getBean('monitorService') } def monitorScrape(){ def message = getMonitorService().serviceMethod() render message } }

© www.soinside.com 2019 - 2024. All rights reserved.