background

Continuing with the previous article, after adding JSON and XML parsers to a project, what type of data does a request get when it does not set the PRODUCES property? This article explores this question.

Reference the XML parsing package

<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> The < version > 2.9.0 < / version > < / dependency >Copy the code

Defining test classes

Define a test Controller and add a method:

@RestController public class TestJson { @RequestMapping(value="/hello/json") @ResponseBody public MyData hello(){ return  new MyData("wang",System.currentTimeMillis()); } @Data class MyData { private String name; private Long time; public MyData(String name,Long time) { this.name = name; this.time = time; }}}Copy the code

Proceduces not specified request response value problem

A request is not specifiedproducesWhat type of data does it return? When requested by the browser, the XML type is returned:Because the accept headerapplication/xmlbefore* / *It’s resolved.

Postman returns JSON data:

The revelation of

If you have both XML and JSON message parsers in your project, you need to check all the methods and specify the FACTORIES attribute for them, because the Accept header field on requests from different clients may differ, resulting in a different back-end parser.