简体   繁体   English

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

[英]how to set the controller to respond as plain text in Grails

I need to create an API for monitoring and for that need that the response will be plain text , not json or xml我需要创建一个用于监控的 API,为此需要响应将是纯文本,而不是 json 或 xml

What I did is:我所做的是:

checked my application yaml that it contains text:检查我的应用程序 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

added to my controller: static responseFormats = ['all'] , tried also static responseFormats = ['text']添加到我的控制器: static responseFormats = ['all'] ,也尝试过static responseFormats = ['text']

in my method did:在我的方法中做了:

respond mystring, formats:['text']

But still grails trying to convert it to JSON and throwing http 406 error但仍然 grails 试图将其转换为 JSON 并抛出 http 406 错误

How can I configure my controller to work with plain text如何配置我的控制器以使用纯文本

Thanks谢谢

I figure out how to do it i used render instead of respond in my method which solved the problem, still not sure why it is not possible with respond我想出了如何做到这一点我使用渲染而不是响应在我的方法中解决了问题,但仍然不确定为什么无法响应

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM