Spring MVC - Using MarshallingView

[Last Updated: Sep 20, 2017]

This example demonstrates how to use MarshallingView for rendering responses. MarshallingView uses a Marshaller delegator which performs Object XML mapping process.

MarshallingView implements View, that means we will still need a ViewResolver which can work with MarshallingView. In following example, we are going to use BeanNameViewResolver (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 Model.


Additional Maven dependencies



spring-oxm provides support for Object/XML Marshalling.

Java Config class

public class MyWebConfig extends WebMvcConfigurerAdapter {

  public void configureViewResolvers(ViewResolverRegistry registry) {

  public View marshallingView() {
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

      MarshallingView view = new MarshallingView();
      return view;

Note that, in above configuration, ViewResolverRegistry#beanName() is equivalent to registering BeanNameViewResolver as a bean;

An object to be marshalled

public class User {
  private String name;
  private String address;
  private String email;

A Controller

public class MyController {

  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");
      return user;

Note that, for MarshalingView to work, the Object to be marshalled is supplied as a parameter in the model (as seen above), which is detected during response rendering.


MarshallingHttpMessageConverter vs MarshallingView

MarshallingHttpMessageConverter as an implementation of HttpMessageConverter, converts HTTP request body containing an XML message to a Java object or a Java object to 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.

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.10.RELEASE: Spring Web MVC.
  • spring-oxm 4.3.10.RELEASE: Spring Object/XML Marshalling.
  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

MarshallingView Example Select All Download
  • marshalling-view-example
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also