How to Handle XML Requests in Spring Boot Controllers with Message Converters

Learn how to quickly wrap XML requests into objects and return XML responses in Spring Boot by leveraging the HttpMessageConverter mechanism, adding the Jackson XML message converter, defining POJOs with Jackson annotations, and creating controller endpoints that consume and produce XML.

Programmer DD
Programmer DD
Programmer DD
How to Handle XML Requests in Spring Boot Controllers with Message Converters

In previous Spring Boot tutorials we only covered handling HTML and JSON request/response formats. This article explains how to quickly wrap XML requests into objects in a controller and return XML responses.

Implementation principle: Message Converter

Spring Boot uses Spring MVC, which relies on the HttpMessageConverter interface to convert between HTTP request/response bodies and Java objects. The interface defines methods for type support, reading, and writing:

public interface HttpMessageConverter<T> {
    boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
    boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);
    List<MediaType> getSupportedMediaTypes();
    T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
    void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
}

To support XML, Spring MVC provides MappingJackson2XmlHttpMessageConverter , which is automatically registered when the jackson-dataformat-xml dependency is present.

Extension implementation

Step 1: Introduce XML message converter

In a traditional Spring application you can add the converter manually:

@Configuration
public class MessageConverterConfig1 extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
        builder.indentOutput(true);
        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
    }
}

In a Spring Boot application you only need to add the following Maven dependency; Spring Boot will auto‑configure the converter:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

Step 2: Define the object‑XML relationship

Define a POJO that maps to the XML structure using Lombok and Jackson XML annotations:

@Data
@NoArgsConstructor
@AllArgsConstructor
@JacksonXmlRootElement(localName = "User")
public class User {
    @JacksonXmlProperty(localName = "name")
    private String name;

    @JacksonXmlProperty(localName = "age")
    private Integer age;
}

The annotations @Data, @NoArgsConstructor, and @AllArgsConstructor are from Lombok and generate getters, setters, and constructors. @JacksonXmlRootElement and @JacksonXmlProperty define how the fields map to XML elements.

Example XML that matches the User class:

<User>
    <name>aaaa</name>
    <age>10</age>
</User>

Step 3: Create an endpoint that receives and returns XML

Implement a controller method that consumes and produces XML:

@Controller
public class UserController {
    @PostMapping(value = "/user", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public User create(@RequestBody User user) {
        user.setName("didispace.com : " + user.getName());
        user.setAge(user.getAge() + 100);
        return user;
    }
}

Start the Spring Boot application and use a tool like Postman to POST the example XML to /user. The service will return the processed XML with the modified name and age.

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.

Spring BootXMLJacksonrestMessage Converter
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.