You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

CXF has an initial implementation of JAX-RS (JSR-311): Java API for RESTfulWeb Services. JAX-RS provides a more standard way to build RESTful services in Java.

A demo called restful_jaxrs can be found in CXF distribution (CXF 2.1 only).

Understanding the basics

Resource class

A resource class is a Java Class annotated with JAX-RS annotations to represent a Web resource. A typical resource class in JAX-RS looks like below:

package demo.jaxrs.server;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.UriParam;
import javax.ws.rs.UriTemplate;
import javax.ws.rs.core.HttpContext;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@UriTemplate("/customerservice/")
public class CustomerService {

    public CustomerService() {
    }

    @HttpMethod("GET")
    @UriTemplate("/customers/{id}/")
    public Customer getCustomer(@UriParam("id") String id) {
        ......
    }

    @HttpMethod("PUT")
    @UriTemplate("/customers/")
    public Response updateCustomer(Customer customer) {
        ......
    }

    @HttpMethod("POST")
    @UriTemplate("/customers/")
    public Response addCustomer(Customer customer) {
        ......
    }

    @HttpMethod("DELETE")
    @UriTemplate("/customers/{id}/")
    public Response deleteCustomer(@UriParam("id") String id) {
        ......
    }

    @UriTemplate("/orders/{orderId}/")
    public Order getOrder(@UriParam("orderId") String orderId) {
       ......
    }
}

URI Templates

@UriTemplate annotation can be applied to resource classes or methods. JAX-RS spec section 2.3 explains the usage of URI Templates.

@HttpMethod

@HttpMethod specifies the HTTP verb (GET, PUT, POST,DELETE) a method can handle.

Sub-resource locators.

A method of a resource class that is annotated with @UriTemplate becomes a sub-resource locator when @HttpMethod is not present. Sub-resource locators are used to further resolve the object that will handle the request. In the example below, getOrder method is a sub-resource locator:

@UriTemplate("/customerservice/")
public class CustomerService {

    @UriTemplate("/orders/{orderId}/")
    public Order getOrder(@UriParam("orderId") String orderId) {
       ......
    }
}

@XmlRootElement(name = "Order")
public class Order {
    private long id;
    private String description;

    public Order() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String d) {
        this.description = d;
    }

    @HttpMethod("GET")
    @UriTemplate("products/{productId}/")
    public Product getProduct(@UriParam("productId")int productId) {
       ......
    }
}

A HTTP GET request to http://localhost:9000/customerservice/orders/223/products/323 is dispatched to getOrder method first. If the Order resource whose id is 223 is found, then Order 223 will be used to further resolve Product resource. Eventually, a Product 323 that belongs to Order 223 is returned.

Content types.

More information on how to use JAX-RS annotations to create a resource class can be found from JAX-RS spec .

Configuring JAX-RS services

Configuring JAX-RS services programmatically

You can create a JAX-RS RESTful service by using JAXRSServerFactoryBean:

JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(CustomerService.class);
sf.setAddress("http://localhost:9000/");
sf.create();

A couple things to note:

  • The JAXRSServerFactoryBean creates a Server inside CXF which starts listening for requests on the URL specified.
  • By default, the JAX-RS runtime is responsible for the lifecycle of resource classes, default lifecycle is per-request. You can set the lifecycle to singleton by using following line:
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider());
    
  • If you prefer not to let the JAX-RS runtime to handle the resource class lifecycle for you (for example, it might be the case that your resource class is created by other containers such as Spring), you can do following:
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    CustomerService cs = new CustomerService();
    sf.setServiceBeans(cs);
    sf.setAddress("http://localhost:9080/");
    sf.create();     
    

Configuring JAX-RS services in container with Spring configuration file.

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/beans.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<display-name>CXF Servlet</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">

  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  
  <jaxrs:server id="customerService" address="/">
    <jaxrs:serviceBeans>
      <bean class="demo.jaxrs.server.CustomerService" />
    </jaxrs:serviceBeans>		   
  </jaxrs:server>

</beans>
  • No labels