This example demonstrates how to use MarshallingView for rendering responses. MarshallingView uses a Marshaller delegator which performs the Object-to-XML mapping process.
MarshallingView implements View, which means we will still need a ViewResolver that can work with MarshallingView. In the following example, we are going to use BeanNameViewResolver (see example here), which will require the MarshallingView to be registered as a bean. The bean name will be used as the view name. The Java object to be marshalled is required to be provided as an attribute of the Model.
Example
Additional Maven dependencies
pom.xml<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>7.0.6</version>
</dependency>
spring-oxm provides support for Object/XML Marshalling.
Java Config class
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.beanName();
}
@Bean(name = "xmlView")
public View marshallingView() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(User.class);
MarshallingView view = new MarshallingView();
view.setMarshaller(marshaller);
return view;
}
}
Note that, in the above configuration, ViewResolverRegistry#beanName() is equivalent to registering BeanNameViewResolver as a bean.
An object to be marshalled
@XmlRootElement(name = "user")
public class User {
private String name;
private String address;
private String email;
.............
}
A Controller
@Controller
@RequestMapping("/")
public class MyController {
@RequestMapping
public String handleRequest(Model model) {
model.addAttribute("user", getUser());
return "xmlView";
}
private User getUser() {
User user = new User();
user.setName("Peggy Newman");
user.setAddress("9626 Levert Ave, Augusta");
user.setEmail("peggy@example.com");
return user;
}
}
Note that, for MarshallingView to work, the object to be marshalled is supplied as a parameter in the model (as seen above), which is detected during response rendering.
Following example shows how MarshallingView works.
The above diagram shows the request flow from top to bottom. Purple boxes are Spring MVC infrastructure, teal boxes are the marshalling part, and neutral boxes are the client and your own controller.
- Controller: it puts the
User object in the model and returns the view name xmlView. It does not touch XML at all.
- View resolution:
BeanNameViewResolver looks up the Spring bean named xmlView, which is the MarshallingView registered in MyWebConfig.
- Rendering:
MarshallingView scans the model for the first entry that its Marshaller supports, or uses the modelKey if you set one. It skips BindingResult entries.
- Marshalling:
Jaxb2Marshaller writes that object as XML to the response with content type application/xml.
Running the example
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Output
$ curl -s "http://localhost:8080" <?xml version="1.0" encoding="UTF-8" standalone="yes"?><user><address>9626 Levert Ave, Augusta</address><email>peggy@example.com</email><name>Peggy Newman</name></user>
MarshallingHttpMessageConverter vs MarshallingView
MarshallingHttpMessageConverter, as an implementation of HttpMessageConverter, converts an HTTP request body containing an XML message to a Java object, or a Java object to an XML response body. Check out this tutorial to get familiar with HttpMessageConverter.
MarshallingView, as an implementation of View, renders the response as XML, as explained in this tutorial.
Deprecations and Removals
As of Spring Framework 6.x and 7.x, the following points are relevant:
- Document View package deprecated (Spring 7.0): The entire
org.springframework.web.servlet.view.document package (including AbstractPdfView, AbstractXlsView, etc.) is officially deprecated. No direct replacement is provided; document libraries (e.g., OpenPDF, Apache POI) are expected to ship their own View adapters, or you should render directly in your handler.
- ResourceBundleViewResolver and XmlViewResolver deprecated (Spring 5.3+): These view resolvers are deprecated in favor of Spring's common view resolver variants and/or custom resolver implementations.
BeanNameViewResolver used in this tutorial is not deprecated.
- MarshallingView is not deprecated: It remains a supported part of Spring MVC in Spring 6.2.x and 7.0.
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 4.1.0.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- spring-oxm 7.0.6 (Spring Object/XML Marshalling)
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- jakarta.xml.bind-api 4.0.5 (Jakarta XML Binding API documentation)
- jaxb-impl 4.0.9 (Old JAXB Runtime module. Contains sources required for runtime processing)
- JDK 25
- Maven 3.9.11
|