How to Make a Spring Boot Controller Return JSON, XML, and YAML Dynamically

Learn how to configure Spring Boot 3.2.5 to let a single controller endpoint dynamically produce JSON, XML, or custom YAML responses using Accept headers, request parameters, path extensions, and custom HttpMessageConverters, with step‑by‑step code examples and necessary Maven dependencies.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Make a Spring Boot Controller Return JSON, XML, and YAML Dynamically

Environment: SpringBoot 3.2.5

1. Introduction

In Spring Boot, a Controller handles HTTP requests and returns responses. Spring provides several matching strategies to define the relationship between requests and handler methods.

Typical RESTful API design guidelines include:

Use nouns to represent resources, e.g. /users, /products.

Use HTTP methods (GET, POST, PUT, DELETE, etc.) to represent operations on resources.

Use path variables for subsets or specific instances, e.g. /users/{userId}.

Use query parameters for filtering or sorting, e.g. /users?name=John&sort=asc.

These conventions keep APIs consistent and readable for front‑end developers.

Although JSON is the default response format, projects may require XML or custom formats. Spring MVC supports JSON, XML, and custom formats out of the box.

2. Practical Examples

2.1 Request Header

Use the Accept header to specify the desired response format.

@RestController
@RequestMapping("/returnformat")
public class ReturnFormatController {
    @GetMapping("")
    public User format() {
        User user = new User(666L, "张三");
        return user;
    }
}

Send a request with the Accept header set to application/xml in Postman.

Postman Accept header example
Postman Accept header example

2.2 Request Parameter

Enable parameter‑based content negotiation in application.yml:

spring:
  mvc:
    contentnegotiation:
      favor-parameter: true

Then request /returnformat?format=xml to get XML. The parameter name can be changed:

spring:
  mvc:
    contentnegotiation:
      favor-parameter: true
      parameter-name: fmt
Parameter based format selection
Parameter based format selection

2.3 Path Extension (Deprecated)

Path‑extension negotiation is discouraged. For Spring 5.3 and earlier it can be enabled with:

spring:
  mvc:
    contentnegotiation:
      favor-path-extension: true

From Spring 6.0 onward you must implement WebMvcConfigurer:

@Configuration
public class FavorPathConfigurer implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(true);
    }
}

Controller example:

@RestController
@RequestMapping("/favors")
public class FavorPathController {
    @GetMapping("/p.*")
    public User favor() {
        return new User(1L, "张三");
    }
}
Path extension example
Path extension example

2.4 Custom Format (YAML)

Implement a custom HttpMessageConverter to output YAML:

public class YamlHttpMessageConverter implements HttpMessageConverter<Object> {
    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return User.class.isAssignableFrom(clazz);
    }
    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return List.of(new MediaType("application", "yaml"));
    }
    @Override
    public void write(Object t, MediaType contentType, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        StreamUtils.copy(new org.yaml.snakeyaml.Yaml().dump(t), StandardCharsets.UTF_8, outputMessage.getBody());
    }
}

Add the media type to configuration:

spring:
  mvc:
    contentnegotiation:
      media-types:
        yaml: application/yaml

After registration, the endpoint can return YAML, which can be deserialized back to a User object.

YAML output example
YAML output example

These steps enable a Spring Boot controller to return JSON, XML, or any custom format such as YAML.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendjavaspring-bootcontent-negotiationrest-api
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.