...
Code Block | ||||
---|---|---|---|---|
| ||||
package demo.jaxrs.server; import java.util.HashMap; import java.util.Map; import javax.ws.rs.HttpMethodGET; import javax.ws.rs.ProduceMime; import javax.ws.rs.UriParamPath; import javax.ws.rs.UriTemplatePathParam; import javax.ws.rs.core.HttpContextContext; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; @UriTemplate@Path("/customerservice/") public class CustomerService { public CustomerService() { } @HttpMethod("GET")@GET @UriTemplate@Path("/customers/{id}/") public Customer getCustomer(@UriParam@PathParam("id") String id) { ...... } @HttpMethod("PUT")@PUT @UriTemplate@Path("/customers/") public Response updateCustomer(Customer customer) { ...... } @HttpMethod("POST")@POST @UriTemplate@Path("/customers/") public Response addCustomer(Customer customer) { ...... } @HttpMethod("DELETE")@DELETE @UriTemplate@Path("/customers/{id}/") public Response deleteCustomer(@UriParam@PathParam("id") String id) { ...... } @UriTemplate@Path("/orders/{orderId}/") public Order getOrder(@UriParam@PathParam("orderId") String orderId) { ...... } } |
...
@Path
@UriTemplate @Path annotation is applied to resource classes or methods. The value of @UriTemplate @Path annotation is a relative URI path follows the URI Template format. More information about URI Templates Path annotations can be found from JAX-RS spec section 2.3.
@HttpMethod
HTTP Method
JAX-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@HttpMethod specifies the HTTP verb (GET, PUT, POST,DELETE) a method can accept.
Sub-resource locators.
A method of a resource class that is annotated with @UriTemplate @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:
Code Block | ||||
---|---|---|---|---|
| ||||
@UriTemplate@Path("/customerservice/") public class CustomerService { @UriTemplate@Path("/orders/{orderId}/") public Order getOrder(@UriParam@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; } @HttpMethod("GET")@GET @UriTemplate@Path("products/{productId}/") public Product getProduct(@UriParam@PathParam("productId")int productId) { ...... } } |
...
In the example below, the Customer object returned by getCustomer is marshaled using JAXB data binding:
Code Block | ||||
---|---|---|---|---|
| ||||
@UriTemplate@Path("/customerservice/") public class CustomerService { @HttpMethod("GET")@GET @UriTemplate@Path("/customers/{customerId}/") public Customer getCustomer(@UriParam@PathParam("customerId") String id) { .... } } |
The wire representation of Customer object is:
Code Block | ||||
---|---|---|---|---|
| ||||
@UriTemplate@Path("/customerservice/") <Customer> <id>123</id> <name>John</name> </Customer> |
...
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@Path("/customerservice/") public class CustomerService { @HttpMethod("GET")@GET @UriTemplate@Path("/customers/") public Customers getCustomers() { .... } } |
...
Following code returns a Customer object that is marshaled to JSON format:
Code Block |
---|
@UriTemplate@Path("/customerservice/") public class CustomerService { @ProduceMime("application/json") @HttpMethod("GET")@GET @UriTemplate@Path("/customers/{customerId}/") public Customer getCustomer(@UriParam@PathParam("customerId") String id) { .... } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
{"Customer ":{"id":"123","name":"john"}} |
...
Source support
You can also receive the request as a DOMSource Source object or return a DOMSource Source object as response:
Code Block | ||||
---|---|---|---|---|
| ||||
@UriTemplate@Path("/customerservice/") public class CustomerService { @HttpMethod("PUT")@PUT @UriTemplate@Path("/customers/") public javax.xml.transform.dom.DOMSourceSource updateCustomer(javax.xml.transform.dom.DOMSourceSource ds) { .... } } |
Secure JAX-RS services
...