This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12028|Reply: 1

[Source] The Spring jackson-dataformat-xml settings interface returns JSON format by default

[Copy link]
Posted on 2022-1-11 10:39:32 | | | |
After introducing the jackson-dataformat-xml package, all GET requests through the browser return data types from the original JSON to XML format, as shown in the figure below:



Accept in Request Headers reads text/html,application/xhtml+xml,application/xml; q=0.9,image/avif,image/webp,image/apng,*/*; q=0.8,application/signed-exchange; v=b3; q=0.9, it seems to be a matter of content negotiation, according to the browser's request header order, if the server introduces MediaType support for XML, it should indeed return XML documents first.

How do I return the default JSON format while using XML dependencies?

Solution 1

Add the following comment to the controller or method to force the return of JSON format:


Although this solution can solve the problem, it requires a lot of code modification.

Solution 2


Set the default ContentType, override the configureContentNegotiation method via implements WebMvcconfigurer, and ignore the Accept header, with the following code:

After modification, the renderings are as follows:



(End)




Previous:Spring Cloud Gateway route yml and json configuration
Next:CentOS installs the zlib-1.2.9 package
 Landlord| Posted on 2022-2-11 10:18:55 |
The Spring Boot settings interface returns HTML information

If you set produces = MediaType.TEXT_HTML_VALUE

The error will be as follows:

Could not find acceptable representation

Final Solution:

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com