Newsletter

JAX-RS XML Example With JAXB Using Jersey

Web Services » on Jul 25, 2014 { 7 Comments } By Sivateja

In this article i will give you an example on how a RESTful web service produces XML response using Jersey. Basically JAX-RS supports conversion of java objects into XML with the help of JAXB. As Jersey it self contains JAXB libraries we no need to worry about JAXB-Jersey integration stuff.

Steps Need to be Followed

  • Add ‘jersey-server.jar‘ to your Maven pom.xml which includes all JAXB supporting libraries into your class path
  • Annotate your service method with @Produces(MediaType.APPLICATION_XML)

Required Files

  • pom.xml
  • web.xml
  • Customer.java
  • RestfulXMLExample.java

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestfulXMLExample</groupId>
  <artifactId>RestfulXMLExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        
    </dependencies>
 
      <build>
        <finalName>RestfulXMLExample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerVersion>1.5</compilerVersion>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>RestPathAnnotationExample</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.java4s</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

Customer.java

package com.java4s;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name = "customer")
public class Customer {
 
    String custName;
    String custCountry;
    int custId;

    @XmlElement
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    
    @XmlElement    
    public String getCustCountry() {
        return custCountry;
    }
    public void setCustCountry(String custCountry) {
        this.custCountry = custCountry;
    }
    
    @XmlAttribute
    public int getCustId() {
        return custId;
    }
    public void setCustId(int custId) {
        this.custId = custId;
    }
}

RestfulXMLExample.java

package com.java4s;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
 
@Path("/customers")
public class RestfulXMLExample {
 
    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Customer getCustomerDetails(@PathParam("id") int custId) {
        
        
     // WRITE DATABASE LOGIC TO RETRIEVE THE CUSTOMER RECORD WITH 'custID'
        
        Customer cust = new Customer();
        cust.setCustName("Java4s");
        cust.setCustCountry("USA");
        cust.setCustId(custId);
 
        return cust;        
     }
}

 

Output

 

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

7 Responses to “JAX-RS XML Example With JAXB Using Jersey”
  1. Pranab Kumar Sahoo says:

    Thanks Sivateja
    Nice

  2. venu says:

    Can you please post one example on Restful web service security.

  3. varun says:

    Can you please post one example on web services security

  4. vajreshwari says:

    Hi sivateja,

    I want to pass the query parameter as Map<String,List>. can u plz suggest how to pass these parameters.

  5. trinadh says:

    Please send more rest full web services in jersey examples in my mail id ASAP

  6. amar says:

    Thanks Sivateja !! your tutorial is so easy to understand. Thanks a lot! keep it up…

  7. kishor says:

    Purpose of @XmlRootElement ..can you please explain

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.