|
Page Edited :
CXF20DOC :
JAX-RS (JSR-311)
JAX-RS (JSR-311) has been edited by Sergey Beryozkin (Apr 14, 2008). Content: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. JAX-RS related demos are located under samples\jax_rs directory (CXF 2.1 only). Understanding the basicsYou are encouraged to read JAX-RS spec Resource classA 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.GET; import javax.ws.rs.ProduceMime; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; @Path("/customerservice/") public class CustomerService { public CustomerService() { } @GET @Path("/customers/{id}/") public Customer getCustomer(@PathParam("id") String id) { ...... } @PUT @Path("/customers/") public Response updateCustomer(Customer customer) { ...... } @POST @Path("/customers/") public Response addCustomer(Customer customer) { ...... } @DELETE @Path("/customers/{id}/") public Response deleteCustomer(@PathParam("id") String id) { ...... } @Path("/orders/{orderId}/") public Order getOrder(@PathParam("orderId") String orderId) { ...... } } @Path@Path annotation is applied to resource classes or methods. The value of @Path annotation is a relative URI path follows the URI Template format. More information about Path annotations can be found from JAX-RS spec HTTP MethodJAX-RS specification defines a number of annotations such as @GET, @PUT, @POST and @DELETE. Using an @HttpMethod designator, one can create a custom annotation such as @Update or @Patch Sub-resource locators.A method of a resource class that is annotated with @Path becomes a sub-resource locator when an annotation with an @HttpMethod designator 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: @Path("/customerservice/") public class CustomerService { @Path("/orders/{orderId}/") public Order getOrder(@PathParam("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; } @GET @Path("products/{productId}/") public Product getProduct(@PathParam("productId")int productId) { ...... } } A HTTP GET request to http://localhost:9000/customerservice/orders/223/products/323 Content type negotiationOne of the coolest thing of REST is that the same resource can be served using multiple representations. @ProduceMime and @ConsumeMime annotations are used to declare the supported request and response media types. JAXB supportThe request and response can be marshaled and unmarshaled to/from Java object using JAXB. The Java object needs to be marked with @XmlRootElement annotation. For example: @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: @Path("/customerservice/") public class CustomerService { @GET @Path("/customers/{customerId}/") public Customer getCustomer(@PathParam("customerId") String id) { .... } } The wire representation of Customer object is: @Path("/customerservice/")
<Customer>
<id>123</id>
<name>John</name>
</Customer>
To work with collections, you need to define an object collection type. For example: @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; } } @Path("/customerservice/") public class CustomerService { @GET @Path("/customers/") public Customers getCustomers() { .... } } JSON supportFollowing code returns a Customer object that is marshaled to JSON format: @Path("/customerservice/") public class CustomerService { @ProduceMime("application/json") @GET @Path("/customers/{customerId}/") public Customer getCustomer(@PathParam("customerId") String id) { .... } The wire representation of Customer object is: {"Customer ":{"id":"123","name":"john"}}
Source supportYou can also receive the request as a Source object or return a Source object as response: @Path("/customerservice/") public class CustomerService { @PUT @Path("/customers/") public javax.xml.transform.Source updateCustomer(javax.xml.transform.Source ds) { .... } } Secure JAX-RS servicesA demo called samples\jax_rs\basic_https shows you how to do communications using HTTPS. Configuring JAX-RS servicesConfiguring JAX-RS services programmaticallyYou 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:
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> |
Unsubscribe or edit your notifications preferences
