Close

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.

Example

Additional Maven dependencies

pom.xml

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-oxm</artifactId>
   <version>4.3.10.RELEASE</version>
</dependency>

spring-oxm provides support for Object/XML Marshalling.

Java Config class

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void configureViewResolvers(ViewResolverRegistry registry) {
      registry.beanName();
  }

  @Bean("xmlView")
  public View marshallingView() {
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
      marshaller.setClassesToBeBound(User.class);

      MarshallingView view = new MarshallingView();
      view.setMarshaller(marshaller);
      return view;
  }
}

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

An object to be marshalled

@XmlRootElement
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 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.

Output

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
                • MyWebConfig.java

    See Also