Content Negotiation in Spring Boot

 Content Negatiation:

  • Same Resource - Same URI
    • with Same URI we can have different Representations are possible
      • we might get Response in JSON format 
      • some other consumer might expect that response in XML formate
in given below example we have same response just the difference is JSON and XML formate 



so one consumer can get in xml or other one can get response in json formate

Example 1 Different Content Type - XML or JSON or ...
Example 2 Different Language - English or Hindi or Urdu ...

Actually your consumer has to tell us which representation they want from us
Q: How can a customer tell the rest api provider what they want as response?
Answer: Content Negotiation

Example 1 : A consumer can use things like Accept Header, i can sent a request , with  a mime type of application XML. so i can create accept Header with value application xml


Example 2: Another Example is to make use of the Accept Language Header(en, nl,fr, hi,ur,...)
so with the same resource we can have different representations

So with Content Negotiation:
Consumer Tells the Content provider what kind of Representation i would want
Xml Response or Jsoan Response  or text back in france or german or english langauage 
and This is typicaly done by your Requst Header

Example : to get response represent in XML format 
Step 1: 
<dependency>

<groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

</dependency>

Step 2: 

Add Header in postman : 


Now we will get xml representation 


so for this Same Resource we are able to able to represent two ways one is JSON and another is XML

Comments

Popular posts from this blog

Introduction of RESTful Web Service

Learn JPA and Hibernate

Implementing Dynamic Filtering for Rest API