Backend Development 7 min read

How to Output XML in Spring Boot 3.1.7 Using Jackson, JAXB, and XML Views

This tutorial explains when to choose XML over JSON, compares their characteristics, and provides step‑by‑step Spring Boot 3.1.7 examples—including Jackson XML configuration, custom root elements, XML view rendering, and JAXB marshalling—to produce XML responses from REST endpoints.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Output XML in Spring Boot 3.1.7 Using Jackson, JAXB, and XML Views

1. Introduction

XML and JSON are both data‑exchange formats, but they differ in readability, standardization, data binding, and type support, which influences the choice between them. XML offers better human readability with tag‑based structure, strong standards like XML Schema, XSLT, XPath, and built‑in data types, making it suitable for legacy systems such as banking interfaces.

2. Practical Examples

2.1 Using Jackson

Add the Jackson XML dependency:

<code>&lt;dependency&gt;
  &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
  &lt;artifactId&gt;jackson-dataformat-xml&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

Define a simple data model:

<code>public class Message {
  private String title;
  private String content;
}</code>

Create a controller that returns the model:

<code>@RestController
@RequestMapping("/mfc")
public class MessageFormatController {
  @GetMapping("/index")
  public Object index() {
    return new Message("标题", "内容");
  }
}</code>

When the application starts, Spring detects the Jackson XML library and automatically registers MappingJackson2XmlHttpMessageConverter , so the endpoint returns XML without extra configuration.

<code>public class WebMvcConfigurationSupport {
  private static final boolean jackson2XmlPresent;
  static {
    jackson2XmlPresent = ClassUtils.isPresent(
      "com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
  }
  protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> converters) {
    if (jackson2XmlPresent) {
      Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
      if (this.applicationContext != null) {
        builder.applicationContext(this.applicationContext);
      }
      converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
    }
  }
}</code>

Customizing XML element names:

<code>@JacksonXmlRootElement(localName = "msg")
public class Message {
  private String title;
  @JacksonXmlProperty(localName = "body")
  private String content;
}</code>

2.2 Using XML View Technology

<code>@RestController
@RequestMapping("/jaxb")
public class JaxbController {
  @GetMapping("/xml")
  public ModelAndView xml() {
    MappingJackson2XmlView view = new MappingJackson2XmlView();
    ModelAndView model = new ModelAndView(view);
    Map<String, Object> modelMap = new HashMap<>();
    modelMap.put("user", new User(66, "张三", "女"));
    modelMap.put("zs", new User(55, "李四", "男"));
    view.setModelKey("zs");
    model.addAllObjects(modelMap);
    return model;
  }
}</code>

Data model for the view:

<code>@XmlRootElement(name = "user")
public class User {
  private Integer age;
  private String name;
  private String sex;
}</code>

2.3 Using JAXB

<code>@GetMapping("/marshaller")
public MarshallingView marshaller() {
  Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
  marshaller.setClassesToBeBound(User.class);
  MarshallingView view = new MarshallingView(marshaller);
  view.getAttributesMap().put("user", new User(22, "张三", "男"));
  return view;
}</code>

Support for nested properties:

<code>@XmlRootElement(name = "user")
public class User {
  private Integer age;
  private String name;
  private String sex;
  @XmlElement(name = "address")
  private Address address = new Address("四川", "成都");
}</code>

Required Maven dependencies for JAXB:

<code>&lt;dependency&gt;
  &lt;groupId&gt;jakarta.xml.bind&lt;/groupId&gt;
  &lt;artifactId&gt;jakarta.xml.bind-api&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
  &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

All the above demonstrates how to configure Spring Boot to produce XML responses using different techniques, helping developers choose the appropriate method for their projects.

JavaSpring BootXMLJacksonRESTJAXB
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

login 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.