0
0
Spring Bootframework~5 mins

Content type negotiation in Spring Boot

Choose your learning style9 modes available
Introduction

Content type negotiation helps your app decide what format to send data in, like JSON or XML, based on what the user or client wants.

When your API needs to support multiple response formats like JSON and XML.
When different clients (web browsers, mobile apps) expect data in different formats.
When you want to make your REST API flexible and user-friendly.
When you want to automatically handle 'Accept' headers from HTTP requests.
When you want to avoid writing separate endpoints for each data format.
Syntax
Spring Boot
In Spring Boot, content type negotiation is often handled automatically by using @RestController and returning objects.

You can also configure it with:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorParameter(true)
                  .parameterName("format")
                  .ignoreAcceptHeader(false)
                  .defaultContentType(MediaType.APPLICATION_JSON)
                  .mediaType("json", MediaType.APPLICATION_JSON)
                  .mediaType("xml", MediaType.APPLICATION_XML);
    }
}

Spring Boot automatically supports JSON if Jackson is on the classpath.

You can customize content negotiation by overriding configureContentNegotiation in a WebMvcConfigurer.

Examples
This simple controller returns data. Spring Boot sends JSON by default if the client accepts JSON.
Spring Boot
@RestController
public class MyController {
    @GetMapping("/data")
    public MyData getData() {
        return new MyData("Hello", 123);
    }
}
This config allows clients to add '?format=xml' or '?format=json' to URLs to choose the response format.
Spring Boot
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorParameter(true)
                  .parameterName("format")
                  .ignoreAcceptHeader(true)
                  .defaultContentType(MediaType.APPLICATION_JSON)
                  .mediaType("json", MediaType.APPLICATION_JSON)
                  .mediaType("xml", MediaType.APPLICATION_XML);
    }
}
This method forces XML output regardless of client request headers.
Spring Boot
@RestController
public class MyController {
    @GetMapping(value = "/data", produces = MediaType.APPLICATION_XML_VALUE)
    public MyData getDataXml() {
        return new MyData("Hello", 123);
    }
}
Sample Program

This Spring Boot app has one endpoint '/data' that returns a simple record. The config allows clients to request JSON or XML by adding '?format=json' or '?format=xml' to the URL or by setting the Accept header.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Configuration
class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorParameter(true)
                  .parameterName("format")
                  .ignoreAcceptHeader(false)
                  .defaultContentType(MediaType.APPLICATION_JSON)
                  .mediaType("json", MediaType.APPLICATION_JSON)
                  .mediaType("xml", MediaType.APPLICATION_XML);
    }
}

@RestController
class MyController {
    record MyData(String message, int number) {}

    @GetMapping("/data")
    public MyData getData() {
        return new MyData("Hello, world!", 42);
    }
}
OutputSuccess
Important Notes

Content negotiation uses HTTP headers like Accept or URL parameters to decide response format.

Always test your API with different Accept headers or URL parameters to see the format change.

Make sure you have the right libraries (like Jackson for JSON, JAXB for XML) in your project.

Summary

Content type negotiation lets your app send data in the format the client wants.

Spring Boot supports this automatically but you can customize it with WebMvcConfigurer.

You can use URL parameters or HTTP headers to control the response format.