Java Reference
In-Depth Information
information to get started using the technology. For those who are familiar with the API, feel free to move onto the
next section of the chapter to begin learning the new features that JAX-RS 2.0 has to offer.
Utilizing the JAX-RS API
A JAX-RS application consists of one or more resources and zero or more providers that are configured via
an application-supplied subclass of Application . A Java class that uses JAX-RS annotations to implement a
corresponding web resource is also known as a resource class . A provider class implements one or more JAX-RS
interfaces. As such, JAX-RS resources, and JAX-RS providers contain a myriad of annotations. JAX-RS resources must
contain a constructor that can accept zero or more arguments. Public constructors may include parameters that are
annotated with: @Context , @CookieParam , @HeaderParam , @PathParam , @MatrixParam , or @QueryParam . Let's begin by
taking a look at an example of a JAX-RS web service resource class. The following JAX-RS service can be invoked to
return a list of Jobs entities to be displayed in a String format:
@Path("/jobsService")
public class JobsService {
@EJB
JobsSession jobsSession;
public JobsService(){
}
@GET
@Produces("text/html")
public String getAsHtml() {
List<Jobs> jobList = jobsSession.retrieveJobsSynchronously();
StringBuilder sb = new StringBuilder();
sb.append("<p>");
for (Jobs job:jobList){
sb.append(job.getJobId() + " - " + job.getTitle());
}
sb.append("<p>");
return sb.toString();
}
}
As mentioned previously, JAX-RS applications must be configured via an application subclass of
javax.ws.rs.core.Application . Therefore, if no Application subclass has been provided, then a servlet must be
configured within web.xml to make use of Application . The following servlet resources should be added to the
web.xml file of any Java EE application that will use JAX-RS, and does not already contain a servlet that handles the
application accordingly:
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
 
Search WWH ::




Custom Search