Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Spring Boot RestController output fromat

Spring Boot RestController output fromat

Posted on March 23, 2025 by Toma Velev

Spring Boot decides which format (JSON, XML, Protobuf, etc.) to use when handling a request based on the configuration for content negotiation.

1. Content Negotiation in Spring Boot

Spring Boot decides how to serialize and deserialize request/response bodies using HttpMessageConverters. These converters are automatically registered based on the libraries in the classpath.

2. How Does Spring Boot Choose the Response Format?

Spring Boot determines the response format in this order:

  1. Request Accept Header
    • If the client sends Accept: application/json, Spring returns JSON.
    • If the client sends Accept: application/xml, Spring returns XML.
    • If the client sends Accept: application/x-protobuf, Spring returns Protocol Buffers (if Protobuf support is configured).
  2. Produces Annotation on Controller Methods
    @GetMapping(value = "/user", produces = "application/xml")
    public User getUser() { return new User("John", "Doe"); }
    
    • This forces Spring to always return XML for this endpoint.
  3. Default Fallback
    • If no Accept header is provided, Spring defaults to JSON (if Jackson is on the classpath).

3. How to Enable Different Formats?

JSON (Default in Spring Boot)

  • Uses Jackson (com.fasterxml.jackson.databind.ObjectMapper).
  • Automatically configured if spring-boot-starter-web is included.

XML Support

  • Add the dependency for jackson-dataformat-xml:
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>
    
  • Annotate your model with @XmlRootElement (optional for Jackson).
    @XmlRootElement
    public class User {
        private String firstName;
        private String lastName;
    }
    
  • Spring will return XML if Accept: application/xml is present.

Protocol Buffers (Protobuf)

  1. Add the dependency:
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
    </dependency>
    
  2. Define a .proto file:
    syntax = "proto3";
    message User {
        string first_name = 1;
        string last_name = 2;
    }
    
  3. Generate Java classes using the Protobuf compiler.
  4. Use ProtobufHttpMessageConverter in Spring Boot:
    @Bean
    public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
        return new ProtobufHttpMessageConverter();
    }
    

4. Customizing output format

Spring Boot uses ContentNegotiationConfigurer to configure format selection manually.

Example: Enable XML and Protobuf globally:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
            .favorParameter(true) // Enable format selection via URL parameter (e.g., ?format=xml)
            .parameterName("format")
            .defaultContentType(MediaType.APPLICATION_JSON)
            .mediaType("json", MediaType.APPLICATION_JSON)
            .mediaType("xml", MediaType.APPLICATION_XML)
            .mediaType("proto", MediaType.APPLICATION_OCTET_STREAM);
    }
}

Now, clients can request different formats using:

  • /user?format=json → JSON
  • /user?format=xml → XML
  • /user?format=proto → Protobuf

Conclusion

Spring Boot decides how to map controllers based on:

  • The Accept header sent by the client.
  • The produces attribute on the controller method.
  • The default behavior, which is JSON unless overridden.
  • The available HttpMessageConverters based on dependencies in the classpath.

For More Spring Boot content, checl out here: https://programtom.com/dev/category/software-development/spring/

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)