I also introduced the Jackson-dataformat-xml dependency, which provides Jackson with the role of transforming entity classes into XML. And Jackson itself can convert the entity class to JSON, so that Jackson can convert the entity class to two types of data, and specifically to what kind of data, to see the HTTP request inside the Accept header information, my browser Chrome's accept is accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8, The server then decides whether to return XML or JSON according to the accept, because the browser accept only the last */* is match Application/json, and Application/xml in front of */*, the priority is higher than JSON, Therefore, the direct call with the browser will return the XML format first.
There are two types of solutions:
1. Modify the Accept message when you call the interface, and change to Application/json (Tools like postman)
2. Add Dependency <dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
</dependency>
You can then use the suffix to invoke the relevant interface to get the data in the corresponding format.
Like I have a URL localhost/get/user returns a user data
After adding the above dependencies, if you want to get the XML format, use Localhost/get/user.xml to invoke the interface
If you want to get the JSON format, use Localhost/get/user.json to invoke the interface
It is the principle of the server based on the suffix to actively modify the Accept information
Localhost/get/user.json Localhost/get/user.xml