-3

I want my spring boot application only to return response in json format.

Spring boot decides the response type according to request accept header. It deserializes response according to the header (json, xml or html)

I know that my client works with json only. So there is no point for me to respond in xml and html formats.

If I am capable of returning xml and html responses, I expect my client to receive json response as well. Why do I have to prepare the response in xml, why don't my client is unable to understand json messages?

You may think that you have more than one client, you save work from all of them but I have more than one API as well so I also need to save work from myself as well...

Assume the client is also a spring boot application. There is no point sending xml or json responses since both will deserialize to the same model object. So why is it my duty to prepare html or xml response? I want my application to be as simple as possible and these different response formats bring pointless complexity in my application. Or is it so?

So how can I configure this in spring boot?

Thanks in advance.

I want to disable xml and html responses. Simply return 404 when these kind of requests are received or inform the client that this server only supports json response type (actually there should be a specific status code for this situation)

Or since HTTP/1.1 servers are allowed to return responses which are not acceptable according to the accept headers sent in the request, ignore the accept header and return json response. (https://stackoverflow.com/a/15027174/17968709)

So how can I configure this in spring boot?

ylmzzsy
  • 1
  • 1
  • 1
    You may want to look into [Unsupported Media Type](https://www.baeldung.com/spring-415-unsupported-mediatype) – Ricky Mutschlechner May 22 '23 at 14:25
  • You aren't saving yourself from any work, infact you are actually taking up more work by only returning/forcing JSON. – M. Deinum May 22 '23 at 14:27
  • client's content type might be ```xml``` but at the same time it can ```accept``` ```html``` or other types. I want to ignore ```accept``` header as well – ylmzzsy May 22 '23 at 15:36
  • @RickyMutschlechner and when I use ```consumes = {"application/json"}``` it still returns html response. – ylmzzsy May 22 '23 at 15:42
  • Please provide enough code so others can better understand or reproduce the problem. – Community May 23 '23 at 00:01

1 Answers1

0

I was able to achieve what I want using produces={"application/json"}

see https://www.baeldung.com/spring-rest-custom-media-type for more info

here is the response

ylmzzsy
  • 1
  • 1