To go with the following example user need to download the following libratires along with the JAXB jar also.
The following is the structure of an application.
pom.xml
[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>[/xml]
A
Project Object Model
is the crucial work in Maven. It is a XML record that contains data about the code and design subtle elements utilized by Maven to construct the code. It contains default values for generally extends. Cases for this is the construct catalog, which is focus on; the source registry, that is
src/fundamental/java
; the test source index, that is
src/test/java
.
Customer.java
[java]package com.splessons;
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;
}
}[/java]
RestfulXMLExample.java
[java]package com.splessons;
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) {
Customer cust = new Customer();
cust.setCustName("SPLESSONS");
cust.setCustCountry("USA");
cust.setCustId(custId);
return cust;
}
}[/java]
In the above example customer name and country name has given to generate the output in XML file and the functionality of
@PathParem
to infuse the estimation of URI parameter that characterized in @Path expression, into Java strategy.
web.xml
[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.splessons</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>[/xml]
Now compile the code from the server as click on the project and go to
Run As
and go to
Server
as follows.
When compile the code from the server the XML file will be generated as follows on the server.