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)
|