14. JAX-RSを用いてEJBをRESTfulなリソースとして公開

The JAX-RS 1.1 specification defines a standard API to provide support for RESTful Web services in the Java platform. Just like other Java EE 6 technologies, any POJO can be easily converted into a RESTful resource by adding the @Path annotation. JAX-RS 1.1 allows an EJB to be published as a RESTful entity.

This section publishes an EJB as a RESTful resource. A new method is added to the EJB which will be invoked when the RESTful resource is accessed using the HTTP GET method.

  1. In CustomerSessionBean.java, add a class-level @Path annotation to publish EJB as a RESTful entity. The updated code looks like:
@Stateless
@LocalBean
@Named
@Path("/customers")
public class CustomerSessionBean {

The new annotation is highlighted in bold. Resolve the imports by clicking on the yellow bulb and selecting javax.ws.rs.Path.

The window shown on the right pops up as soon as you save this file.

The “REST Resources Path” is the base URI for servicing all requests for RESTful resources in this web application. The default value of “/resources” is already used by the .xhtml templates and CSS used by JSF. So change the value to “/restful” and click on “OK”.

Notice that a new class that extends javax.ws.rs.core.Application is generated in the org.netbeans.rest.application.config package. This class registers the base URI for all the RESTful resources provided by @Path. The updated base URI is “restful” as can be seen in the generated class.

  1. Add the following method to CustomerSessionBean.java:
@GET
@Path("{id}")
@Produces("application/xml")
public Customer getCustomer(@PathParam("id")Integer id) {
    return (Customer)em.createNamedQuery("Customer.findByCustomerId").setParameter("customerId", id).getSingleResult();
}

This method is invoked whenever the REST resource is accessed using HTTP GET and the expected response type is XML.

ノート

Notice the following points: * The @GET annotation ensures that this method is invoked when the resource is accessed using the HTTP GET method. * The @Path(“{id}”) defines a sub-resource such that the resource defined by this method can be accessed at customers/{id} where “id” is the variable part of the URI and is mapped to the “id” parameter of the method as defined by @PathParam annotation. * The @Produces annotation ensures that an XML representation is generated. This will work as @XmlRootElement annotation is already added to the generated entity.

Fix the imports by taking the default values for all except for @Produces. This annotation needs to be resolved from the javax.ws.rs package as shown.

_images/14-fix-import.png
  1. The RESTful resource is now accessible using the following format:

http://<HOST>:<PORT>/ <CONTEXT-ROOT>/<RESOURCE-BASE-URI>/<RESOURCE-URI>/<SUB-RESOURCE-URI>/<VARIABLE-PART>

The <CONTEXT-ROOT> is the context root of the web application. The <SUB-RESOURCE-URI> may be optional in some cases. The <VARIABLE-PART> is the part that is bound to the parameters in Java method.

In our case, the URI will look like:

http://localhost:8080/JavaEE6SampleApp/restful/customers/{id} where {id} is the customer id shown in the JSF page earlier. So accessing “http://localhost:8080/JavaEE6SampleApp/restful/customers/1” in a browser displays the output as shown.

_images/14-rest-result.png

Accessing this URI in the browser is equivalent to making a GET request to the service. This can be verified by viewing the HTTP headers generated by the browsers as shown (“Tools”, “Developer Tools” in Chrome).

_images/14-headers.png
  1. Each resource can be represented in multiple formats. Change the @Produces annotation in CustomerSessionBean.java from:
@Produces("application/xml")

to

@Produces({"application/xml", "application/json"})

This ensures that an XML or JSON representation of the resource can be requested by a client. This can be easily verified by giving the following command (shown in bold) on a command-line:

Notice the following points:
  • The command is shown in the bold letters.
  • Connection handshake is pre-fixed “*”.
  • The HTTP request headers are pre-fixed with “>” and response headers with “<”.
  • The response in JSON format is at the end of the message.

The “curl” utility for Windows-based machines can be downloaded from: http://curl.haxx.se/.

Project Versions

前のトピックへ

13. 宣言的Ajaxでデーターベースから部分的な値を取得

次のトピックへ

15. JerseyのクライアントAPIを使用してRESTfulなリソースへアクセス

このページ