XML Response with RESTEasy and JAXB

Learn to use Jackson with RESTEasy to convert the API response into XML format with example.

RESTEasy is a JBOSS-provided implementation of Jakarta-RS/JAX-RS specification for building RESTful Web Services. JAXB is used for mapping Java classes to equivalent XML documents and vice versa. It is done using marshalling and unmarshalling features of JAXB.

This post will teach us to use JAXB with RESTEasy to convert the API response into XML format.

1. Maven

Include the latest version of resteasy-jaxb-provider module into project dependencies along with RESTEasy core dependencies.

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
    <version>6.2.1.Final</version>
</dependency>

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-core</artifactId>
  <version>6.2.1.Final</version>
</dependency>
<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-client</artifactId>
  <version>6.2.1.Final</version>
</dependency> 

2. Use Mediatype “application/XML”

Now use the media type “application/xml” in the @Produces annotation to add support for JSON responses.

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import com.howtodoinjava.model.User;

@Path("/user-management")
public class UserManagementModule
{
	@GET
	@Path("/users/{id}")
	@Produces("application/xml")
	public Response getUserById(@PathParam("id") Integer id)
	{
		User user = new User();
		user.setId(id);
		user.setFirstName("Lokesh");
		user.setLastName("Gupta");
		return Response.status(200).entity(user).build();
	}
}

3. Demo

When we deploy above-built application in tomcat and hit the URL: ” http://localhost:8080/RESTfulDemoApplication/user-management/users/10″, below is the response.

RESTEasy JAXB Example

Happy Learning !!

Leave a Comment

  1. 1.)Write a java program to create url and access two functions like [ getoffer() and getvaluefrom() ].
    a.)in getoffer(accesing 3 variables) should be in string format ,which stores values in xml format and it returns in html output.
    b.)in getvaluefrom() should return string from saved xml and once done the xml content should be emptied.

    To test the program we can use advanced rest client.

    Can u provide me with necessary codes for solving the above description .

    Reply
  2. Lokesh , thanx for ur posts its very usefull.
    how can we implement it for a response have multiple user objects(multiple xml elements)

    Reply
  3. Lokesh, I am trying to learn REST webservices. Is it possible to fetch the values from database instead of hardcoding it. Do you have any examples to get the values from db? If so please let me know.

    Reply
    • I am not sure why it is difficult for you. REST works at View layer, once you got the request, you are free to do anything you want with it.. just like any other application. Try first.

      Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.