Friday, January 29, 2021

Spring MVC XML as Response Example

In this post we’ll see how to get XML as response in Spring MVC application. For marshalling and unmarshalling (converting object from/to XML) JAXB is used in this Spring MVC example.

Technologies used

Following is the list of tools used for the Spring MVC XML generation example.

  • Spring 5.0.8 Release (Spring core, spring web, spring webmvc).
  • Java 10
  • Tomcat server V 9.0.10
  • Eclipse IDE
  • JAXB API 2.3.0

Spring MVC Project structure using Maven

Maven Dependencies

Apart from Spring dependencies JAXB dependencies are also to be added to the pom.xml for generation of XML. From JDK 9, JEE modules are deprecated so you will need to add dependencies in your pom.xml for inclusion of JAXB jars previously these jars were part of JDK itself.

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-core</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>javax.activation</groupId>
  <artifactId>javax.activation-api</artifactId>
  <version>1.2.0</version>
</dependency>

Requirement for generating XML in Spring MVC

In order to return XML as response with in your Spring MVC application-

  1. You need to annotate your model bean with JAXB annotations.
  2. @ResponseBody annotation has to be added to the controller's method, with that returned object is serialized to the response body through an HttpMessageConverter.

Spring MVC generate XML as response – Model classes

There are two classes User class whose objects are returned in the XML form and UserListContainer class which contains the List of objects of type User, this class is needed as we are sending a list of Users. These POJOs are annotated with JAXB annotations for defining the XML structure.

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement
@XmlType(propOrder = {"firstName", "lastName", "email"})
public class User {

 private String firstName;
 private String lastName;
 private String email;

 public User() {
  
 }
 public User(String firstName, String lastName, String email) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.email = email;
 }
 
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
}
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="users")
public class UserListContainer {
  private List<User> userList;

  @XmlElement(name = "user")
  public List<User> getUserList() {
    return userList;
  }

  public void setUserList(List<User> userList) {
    this.userList = userList;
  }
}

Spring MVC XML as response – Controller class

@Controller
public class UserController {
  @RequestMapping(value = "/getUsers", method = RequestMethod.GET, produces="application/xml")
  @ResponseBody
  public UserListContainer getUsers(Model model) throws Exception{
    List<User> users = getListOfUsers();
    UserListContainer userList = new UserListContainer();
    userList.setUserList(users);
    return userList;
  }
    
  // Dummy method for adding List of Users
  private List<User> getListOfUsers() throws ParseException {
    List<User> users = new ArrayList<User>();
    users.add(new User("Jack", "Reacher", "abc@xyz.com"));
    users.add(new User("Remington", "Steele", "rs@cbd.com"));
    users.add(new User("Jonathan", "Raven", "jr@sn.com"));
    return users;
  }
}

Here in the handler method you can see a new attribute “produces” with value as “application/xml” with in the @RequestMapping annotation to explicitly specify the MIME media types or representations a resource can produce and send back to the client.

@ResponseBody annotation is also used in the handler method to indicate that the returned object has to be serialized to the response body. Note that return is serialized to the response body through an HttpMessageConverter.

Deploying and testing application

Once the application is deployed to Tomcat server it can be accessed using the URL- http://localhost:8080/spring-mvc/getUsers and XML will be returned as response.

Spring MVC XML generation

That's all for this topic Spring MVC XML as Response Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


Related Topics

  1. Spring MVC Excel Generation Example
  2. Difference Between @Controller And @RestController Annotations in Spring
  3. Spring MVC Example With @PathVaribale - Creating Dynamic URL
  4. Spring MVC - Binding List of Objects Example
  5. Spring MVC Form Example With Bean Validation

You may also like-

  1. Spring Transaction Attributes - Propagation And Isolation Level Settings
  2. Configuring DataSource in Spring Framework
  3. Bean Scopes in Spring With Examples
  4. Spring Expression Language (SpEL) With Examples
  5. Difference Between ArrayList And LinkedList in Java
  6. Java ReentrantLock With Examples
  7. Try-With-Resources in Java With Examples
  8. Zipping Files And Folders in Java

No comments:

Post a Comment