dimanche 28 février 2021

How to force springboot return Json response with global settings

Here are problems. They who are testing my application told me that when they trying to directly input the url into address to query a GET url by chrome. My api will always return a XML formatted response like this. enter image description here

But all the response should be formatted in JSON string ! XML is unacceptable !

I tried to analyze the source code and found request sending by chrome directly will bear attributes like this:

accept= 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
content-type=null

while any other application will send request with:

accept= */* (or accept = application/json)
content-type = application/json

In this case , springboot will then response XML to the request send by chrome because of the missing content-type and specified accept properties.

There is a way to fix this case by excluding jackson for XML in pom. But I don't think it is a graceful way.

I think there are ways more graceful to fix this problem. Help me please. Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire