Content negotiation example using Spring Boot

In this article, we will see Content negotiation example using Spring Boot. First, we will see what is content negotiation, how to configure the content negotiation mechanism in real time development using spring boot. Let’s see what is content negotiation and how to configure it.

Content negotiation example using Spring Boot

  • Content negotiation means what type of data you want to produce or consume. It may be JSON or XML.
  • For configuring content negotiation, we need to add a dependency in pom.xml.
 <dependency>
      		<groupId>com.fasterxml.jackson.dataformat</groupId>
      		<artifactId>jackson-dataformat-xml</artifactId>
  </dependency>
  • We need to add produces = {“application/xml”} in @RequestMapping annotation.
@RequestMapping(value = "/getBook",method = RequestMethod.GET, produces = {"application/json"})
  •  Or we can configure content negotiation globally using WebMvcConfigurerAdapter(A predefined class available in org.springframework.web.servlet.config.annotation package) as below.
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfigAdapter extends WebMvcConfigurerAdapter{
	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		
		configurer.defaultContentType(MediaType.APPLICATION_XML);
		
	}
}

We will have

Let’s see an example where we are going to produce XML data as a response.

Step 1 – open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)click on next.

Step 2 –  Fill all details as below and click on finish.

 

Step 3 – Create the below directory structure, open pom.xml and add the required dependency.

Content negotiation example using Spring Boot

 

 

Modify pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SpringContentNegotiation</groupId>
  <artifactId>SpringContentNegotiation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringContentNegotiation</name>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
 </parent>
  <dependencies>
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
      		<groupId>com.fasterxml.jackson.dataformat</groupId>
      		<artifactId>jackson-dataformat-xml</artifactId>
     </dependency>
   </dependencies>
    

</project>

 

Step 3 – Define classes.

Book.java

package com.contentnegotiation.entity;


public class Book {
	 

	 private int bookId;
	 private String bookName;

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	
	 
	 
}

 

BookController.java

package com.contentnegotiation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.contentnegotiation.entity.Book;



@RestController
@RequestMapping("/book")
public class BookController {

	
	@RequestMapping(value = "/getBook",method = RequestMethod.GET, produces = {"application/xml"})
    @ResponseBody
    public Book getBookDetails() {
		Book book = new Book();
		book.setBookId(1);
		book.setBookName("rich dad poor dad");
		
		return book;
	}
}

 

SpringMain.java

package com.contentnegotiation.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication(scanBasePackages={"com.contentnegotiation.*"})
public class SpringMain {
	public static void main(final String[] args) {
		SpringApplication.run(SpringMain.class, args);
		
		
	}
}

 

Configure WebMvcConfigurerAdapter class and override configureContentNegotiation method.

WebConfigAdapter.java

package com.contentnegotiation.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfigAdapter extends WebMvcConfigurerAdapter{
	/*@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		
		configurer.defaultContentType(MediaType.APPLICATION_XML);
		
	}*/
}

 

Define the port in application.properties.

server.port = 9091

 

Step 4 – Run the SpringMain.java and deploy our application.

Content negotiation example using Spring Boot

 

Step 5 – Test the rest URI.

 

We have the response as XML. Did you notice in the above example we have written  produces = {“application/xml”} in BookController.java and commented the code inside WebConfigureAdapter class.

@Configuration
public class WebConfigAdapter extends WebMvcConfigurerAdapter{
	/*@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		
		configurer.defaultContentType(MediaType.APPLICATION_XML);
		
	}*/
}

In this approach, we need to define  produces = {“application/xml”} with each request URI. Suppose we know our application only can produce XML data as the response. We no need to define produces = {“application/xml”} with each resource. We can configure globally what kind of data we want to produce. Let’s do some small modification in BookController.java and WebConfigAdapter.java.

  • Remove produces = {“application/xml”} from request URI.
  • Override configureContentNegotiation() method in WebConfigAdapter.java class.
  • Run the SpringMain.java and deploy the application. We will have the same output i.e xml data we will have as response.

 

That’s all we have about Content negotiation example using Spring Boot.

you may like –