Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest Grails';内容协商处理对立类型?_Rest_Grails_Content Type_Content Negotiation - Fatal编程技术网

Rest Grails';内容协商处理对立类型?

Rest Grails';内容协商处理对立类型?,rest,grails,content-type,content-negotiation,Rest,Grails,Content Type,Content Negotiation,来自3个不同来源: 接受标题 请求参数(格式) URI扩展 问题是,当它从多个地方获取内容信息时,特别是当它们彼此不一致时,它会做什么 例如,如果Grails收到如下请求,会发生什么情况: URL: http://example.com/book/list.html?format=json Accept: application/xml Accept头将解析为xml,URI扩展将解析为html,参数将解析为json 这会有什么作用: import grails.converters.* cl

来自3个不同来源:

  • 接受标题
  • 请求参数(格式)
  • URI扩展
  • 问题是,当它从多个地方获取内容信息时,特别是当它们彼此不一致时,它会做什么

    例如,如果Grails收到如下请求,会发生什么情况:

    URL: http://example.com/book/list.html?format=json
    Accept: application/xml
    
    Accept头将解析为xml,URI扩展将解析为html,参数将解析为json

    这会有什么作用:

    import grails.converters.*
    
    class BookController {
    
        def list() {
            def books = Book.list()
            withFormat {
                html bookList: books
                xml { render books as XML }
                json { render books as JSON }
            }
        }
    }
    

    对于Grails2.0.0RC3,下面将返回html

    curl -v -H "Accept: application/xml" http://localhost:8080/book/book/list.html?format=json
    
    优先顺序为:

  • URI扩展
  • 格式请求参数
  • 接受标题
  • 请注意,为了使用Accept头,必须在grails app/conf/Config.groovy文件中更改以下参数(默认值为false):

    grails.mime.use.accept.header = true