Pages

July 21, 2014

RESTful Services using Apache CXF

In this article I will detail how to create a REST based web services using Apache CXF without using Spring.
Cf. CXF docs:
JAX-RS: Java API for RESTful Web Services is a Java programming language API that provides support in creating web services according to the Representational State Transfer (REST) architectural style.
CXF 3.0.0 completely implements JAX-RS 2.0 including new Client API and has been fully tested against the first JAX-RS 2.0 TCK

Tools:
- Java 7
- Maven 3
- Apache CXF 3.0.0
- JAX-RS 2.0

Add the following maven dependencies :


      <dependency>
        <groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.0.0-milestone1</version>
      </dependency>
      <dependency>
        <groupId>javax.ws.rs</groupId><artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
      </dependency>
      <dependency>
        <groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId>
        <version>3.0.0-milestone1</version>

      </dependency>
      <dependency>
        <groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-hc</artifactId>
        <version>3.0.0-milestone1</version>

      </dependency>


Let's start with creating a bean class as response of the webservice. Will be annotated @XmlRootElement, to be used as xml/json response:


import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement

@XmlAccessorType(XmlAccessType.FIELD)
public class PersonResponse {
    private String id;
    
private String name;
    
private String age;
    //...
    
//getters, setters
    //...

}

Following code returns a Person object that is marshaled to XML or JSON format:


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;
import javax.ws.rs.core.Response;

@Path(
"/api/service")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class PersonService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path(
"/person//id/{id}/")
    
public Response getPerson(@PathParam("id")String personId) throws Exception{
        PersonResponse response = new PersonResponse();
        // some code here
        return Response.ok().build();
    }

}
In the cas of error use the following:
return Response.status(Response.Status.FORBIDDEN);
This will return a HTTP 403 default message. If we would a customized one, try this:

        ErrorMessageResponse error = new ErrorMessageResponse();
        error.setErrorMessage(
"your message here");
        
return Response.status(Response.Status.FORBIDDEN).type(MediaType.APPLICATION_JSON).entity(error).build();

ErrorMessage Response is a simple bean annotated with @XmlRootElement

Now use tomcat for deploying the webservice, so our servlet declaration in the web.xml will be :

<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
</servlet-class>
    <init-param>
      <param-name>jaxrs.serviceClasses</param-name>
<param-value>com.mypackage.PersonService</param-value>
</init-param>
    <init-param>
      <param-name>jaxrs.address</param-name>
<param-value>/rest</param-value> 
</init-param> 
</servlet>
<servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

You can test your web service, with unit test or soapui.

No comments:

Post a Comment