【问题标题】:Spring request to return json OR xml responseSpring 请求返回 json 或 xml 响应
【发布时间】:2018-07-06 13:29:25
【问题描述】:

我有一个 Spring 请求映射,我希望默认返回 XML,或者如果在请求标头中指定,则返回 JSON。这是一些代码:

请求映射

@RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET, produces = "application/xml)
    public ResponseEntity<JobResults> processTestObject(@PathVariable("progName") String progName,
                                                        @RequestHeader("Content-Type") String contentType) throws Exception {
        HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
        if(contentType.equals("application/json")) {
            responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        }
        LOGGER.info("Running batch program " + progName);
        JobResults response = batchService.processProgName(progName);
        return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
    }
  • 使用没有标头的邮递员,当我点击此端点时,我收到一个 400 Bad Request 状态码。
  • 在 Postman Headers 字段中,如果我将 Content-Type 指定为 application/xml 我得到了正确的 XML 响应。
  • 如果我将 Content-Type 指定为 application/json 我会收到错误 返回:Unexpected '&lt;'

我想要什么:

默认从端点返回 XML,如果在请求中指定则返回 JSON

编辑

到目前为止,当 Postman 中没有发送 AcceptContent-Type 时,请求会返回 400 Bad Request。为了检索所需的响应,我必须将AcceptContent-Type 指定为application/xml

 @RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
    public ResponseEntity<JobResults> processTestObject(@PathVariable("progName") String progName,
                                                        @RequestHeader("Content-Type") MediaType contentType) throws Exception {
        HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_XML);
        if(!contentType.toString().equals("*/*")) {
            responseHeaders.setContentType(contentType);
        }
        LOGGER.info("Running batch program " + progName);
        JobResults response = batchService.processProgName(progName);
        return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
    }

【问题讨论】:

    标签: json xml spring http


    【解决方案1】:

    您的示例仍然从请求中获取Content-Type,这应该是Accept。其次,确保您的 JobResults 对象可被 json marshaller 和 xml marshaller 序列化。第三,如果您想要特定的行为是请求 json,您应该检查该特定媒体类型,而不是像 "*/*" 这样的通用媒体类型。

    控制器:

    @RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
    public ResponseEntity<JobResults> processTestObject(@PathVariable("progName") String progName,
                                                        @RequestHeader("Accept") MediaType accept) throws Exception {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_XML);
        if(!accept.toString().equals("*/*")) {
            responseHeaders.setContentType(accept);
        }
        JobResults response = new JobResults("23423", "result");
        return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
    }
    

    一些响应对象,但已注释。

    @XmlRootElement
    public class JobResults {
        String id;
        String name;
    
        public JobResults(String id, String name) {
            this.id = id;
            this.name = name;
        }
    
        ....
    }
    

    当通过Accept 标头请求时,上面的代码给了我application/json

    【讨论】:

    • Postman 允许您添加自定义标题。 Content-Type 用于描述请求的内容。 Accept-Type 是为了表达你对回应的愿望。
    • 我不确定。当我使用键Accept-Type 和值application/xml 向端点提交请求时,它立即返回400 Bad Request 当我在谷歌上搜索“Accept-Type”时,我找不到任何使用结果
    • 我的错。这是Accept 标头。此页面可能有助于说明问题:baeldung.com/spring-mvc-content-negotiation-json-xml
    • 我已将 RequestMapping 设置为生成 XML 和 JSON,现在我使用 .xml 或 .json 扩展名调用端点。谢谢,这解决了我一半的问题:P
    • 不客气。你的另一半问题是什么? p.s.如果对您有帮助,请考虑接受答案。
    猜你喜欢
    • 2016-05-18
    • 1970-01-01
    • 2012-08-02
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多