Spring MVC Content Negotiation Viewresolver XML JSON Example

This tutorial introduces Spring MVC Content Negotiation Viewresolver XML JSON Example. Spring MVC Content Negotiation features help you response different types of a document at the same URI. The best appropriate response will be served to fit your request. In this tutorial, we will create the example that negotiate between XML and JSON using HTTP Accept header or file extension.

Table of contents:
1. Maven Dependency
2. Project structure
3. Spring MVC Content Negotiation XML JSON Configuration
4. Controller Endpoint
5. Spring MVC Content Negotiation Viewresolver XML JSON Example

Other interesting posts you may like

Maven Dependency
We need to use the following dependencies that supports to create excel document and pdf document.

Project structure

Spring MVC Xml Json Viewresovler Example

Spring MVC Content Negotiation XML JSON Configuration
Here, we need to create the content negotiation manager by extending the WebMvcConfigurerAdapter and override two methods configureContentNegotiation and configureViewResolvers.

The configureContentNegotiation(ContentNegotiationConfigurer configurer) method is used to register a default content type.

The configureViewResolvers(ViewResolverRegistry registry) method is used to register different view resolvers that the application can serve. In this code snippet we register both the MappingJackson2XmlView for serving XML and the MappingJackson2JsonView for serving JSON.

This is the same Spring XML configuration as above.

Notices: we need to use the DispatcherServlet to mapping the request to the correct controller methods. It is the same DispatcherServlet of the previous Spring MVC so it is not mentioned here or you can see it in the source code attachment.

Controller Endpoint
This Fruit POJO is used to add content to the XML and JSON response.

The FruitsController is responsible to create and return an instance of the Fruit class for Model which be used to display in XML or JSON format.

Deploy Spring MVC Content Negotiation Viewresolver XML JSON Example

Building project with maven then deploy file war on application server or servlet container (Tomcat 8 for example).

We access the address with a file extension URL http://localhost:8080/xml-json-viewresolver/viewfruit/1.xml or the address with special request parameter of type xml URL http://localhost:8080/xml-json-viewresolver/viewfruit/1?type=xml, the screen will display the xml response such as the picture

Spring MVC Content Negotiation Viewresolver XML JSON Example viewxml

To view JSON response we need to pass a HTTP “Accept: application/json” header with request. Here we put the URL http://localhost:8080/xml-json-viewresolver/viewfruit/1 in the form DHC plugin on Chrome to pass HTTP “Accept: application/json” header via request. The json content should be such as the below screen shot.

Spring MVC Content Negotiation Viewresolver XML JSON Example viewjson

That’s it on the tutorial Spring MVC Content Negotiation Viewresolver XML JSON Example. You can also refer to other view resolver example such as Spring MVC Excel Pdf ViewResolver Example

Download complete source code of example, please click link below

spring-mvc-xml-json-viewresovler-example.zip (61 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons