...
A demo called restful_jaxrs can be found in CXF distribution (CXF 2.1 only).
Understanding the basics
You are encouraged to read JAX-RS spec to find out informations not covered by this documentation.
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:
...
@HttpMethod specifies the HTTP verb (GET, PUT, POST,DELETE) a method can handleaccept.
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:
Code Block | ||||
---|---|---|---|---|
| ||||
@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 the Order 223 will be used to further resolve Product resource. Eventually, a Product 323 that belongs to Order 223 is returned.
Content type negotiation
One of the coolest thing of REST is that the same resource can be accessed using multiple Representations. @ProduceMime and @ConsumeMime annotations are used to declare the supported request and response media types.
...
JAXB support
To accept or return Objects that can be marshaled/unmarshaled using JAXB, the Objects need to be marked with @XmlRootElement annotation. For example:
Code Block | ||||
---|---|---|---|---|
| ||||
@XmlRootElement(name = "Customer")
public class Customer {
private String name;
private long id;
public Customer() {
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setId(long i) {
id = i;
}
public long getId() {
return id;
}
}
|
In the example below, the Customer object returned by getCustomer is marshaled using JAXB data binding:
Code Block | ||||
---|---|---|---|---|
| ||||
@UriTemplate("/customerservice/")
public class CustomerService {
@HttpMethod("GET")
@UriTemplate("/customers/{customerId}/")
public Customer getCustomer(@UriParam("customerId") String id) {
....
}
}
|
The wire representation of Customer object is:
Code Block | ||||
---|---|---|---|---|
| ||||
@UriTemplate("/customerservice/")
<Customer>
<id>123</id>
<name>John</name>
</Customer>
|
To return or accept Object collections, you need to define an Object collection type. eg:
Code Block |
---|
@XmlRootElement(name = "Customers")
public class Customers {
private Collection<Customer> customers;
public Collection<Customer> getCustomer() {
return customers;
}
public void setCustomer(Collection<Customer> c) {
this.customers = c;
}
}
@UriTemplate("/customerservice/")
public class CustomerService {
@HttpMethod("GET")
@UriTemplate("/customers/")
public Customers getCustomers() {
....
}
}
|
JSON support
Following code returns a Customer Object that is marshaled to JSON format:
Code Block |
---|
@UriTemplate("/customerservice/")
public class CustomerService {
@ProduceMime("application/json")
@HttpMethod("GET")
@UriTemplate("/customers/{customerId}/")
public Customer getCustomer(@UriParam("customerId") String id) {
....
}
|
The wire representation of Customer object is:
Code Block | ||||
---|---|---|---|---|
| ||||
{"Customer ":{"id":"123","name":"john"}}
{code:java}
h3. DOMSource support
{code:java}
@UriTemplate("/bookstore/")
public class BookStore {
@HttpMethod("GET")
@UriTemplate("/customers/{customerId}/")
public javax.xml.transform.dom.DOMSource getCustomer(@UriParam("customerId") String id) {
....
}
}
|
Configuring JAX-RS services
...
- 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:
Code Block java java 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:
Code Block java java 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.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?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> |