Subbareddy
Subbareddy

Reputation: 49

How Should I Make Spring Boot Rest API's Return Response in Client Requested Formate

I have Spring boot Rest APIS, but i want make those APIS Response as per the Client Requested Format.

For Instance :

 Rest APIS Return Response By default In Json format. But Client Want to in XML formate or any other formate.

 In the above situation how can i make my APIS dynamically Retunr the Response as per the Client requested format.

can any tell how can i do this.

Thanks in advance.

Upvotes: 0

Views: 196

Answers (1)

Dylan Morley
Dylan Morley

Reputation: 1726

What you are talking about is Content Negotiation - Here's a Baeldung article that describes how you might use Jackson libraries to handle both XML and JSON data,

https://www.baeldung.com/spring-mvc-content-negotiation-json-xml

You need to devise a content negotiation strategy - very often, that is the use of the Accept header. Your API then has to respect this header and return the appropriate content, which might mean your API contracts having certain attributes to allow it to easily serialize to JSON/XML.

The article linked above links to a github repo that shows how this might be achieved - https://github.com/eugenp/tutorials/tree/master/spring-mvc-basics

Upvotes: 1

Related Questions