Localhost/get/user.json Localhost/get/user.xml

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.