Implementing a REST GET request

Let's now implement the RESTful web service class. Create the CourseService class in the packt.jee.eclipse.rest.ws.services package:

package packt.jee.eclipse.rest.ws.services; 
 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
 
import packt.jee.eclipse.rest.ws.dto.Course; 
import packt.jee.eclipse.rest.ws.dto.Teacher; 
 
@Path("/course") 
public class CourseService { 
 
  @GET 
  @Produces (MediaType.APPLICATION_XML) 
  @Path("get/{courseId}") 
  public Course getCourse (@PathParam("courseId") int id) { 
 
    //To keep the example simple, we will return 
    //hardcoded values here. However, you could get 
    //data from database using, for example, JDO or JDBC 
 
    return new Course(id,"Course-" + id, 5, new Teacher(2, 
"Teacher1")); } }

The @Path annotation specifies that resources made available by this class will be accessible by relative URI "/course".

The getCourse method has many annotations. Let's discuss them one at a time.

The @GET annotation specifies that when the relative URI (as specified by @Path on CourseService class) "/course" is called using the HTTP GET method, then this method will be invoked.

@Produces (MediaType.APPLICATION_JSON) specifies that this method generates a JSON output. If the client specifies the accepted MIME types, then this annotation would be used to resolve the method to be called, if more than one method is annotated with @GET (or, for that matter, any of the other HTTP method annotations). For example, if we have a method called getCourseJSON annotated with @GET , but producing data with different MIME types (as specified by @Produces), then the appropriate method will be selected on the basis of the MIME type requested by the client. The MIME type in the @Produces annotation also tells the JAX-RS implementation MIME type of the response to be created, when marshalling the Java object that is returned from that method. For example, in the getCourse method we return an instance of Course, and the MIME type specified in @Produces tells Jersey to generate an XML representation of this instance.

The @Path annotation can also be used at the method level to specify sub-resources. The value specified in @Path at the method level is relative to the path value specified at the class level. The resource (in this case, Course) with ID 20 can be accessed as /course/get/20. The complete URL can be http://<server-address>:<port>/<app-name>/course/get/10. Parameter names in the path value are enclosed in {} in annotations.

Path parameters need to be identified in method arguments by using the @PathParam annotation and the name of the parameter as its value. The JAX-RS implementation framework matches the path parameters with arguments matching @PathParam annotations and appropriately passes parameter values to the method.

To keep the example simple and to keep the focus on implementation of RESTful web services, we are not going to implement any business logic in this method. We could get data from the database by using, for example, JDO or JDBC APIs (and we have seen examples of how to use these APIs in earlier chapters), but we are just going to return some hardcoded data. The method returns an instance of the Course class. The JAX-RS implementation would convert this object into an XML representation by using JAXB when the data is finally returned to the client.

We need to tell the Jersey framework what packages it needs to scan to look for REST resources. There are two ways to do this:

We will use the second option to create a subclass of Application. However, instead of directly subclassing Application, we will subclass the ResourceConfig class of Jersey, which in turn extends Application.

Create the CourseMgmtRESTApplication class in the packt.jee.eclipse.rest.ws package:

package packt.jee.eclipse.rest.ws; 
 
import javax.ws.rs.ApplicationPath; 
 
import org.glassfish.jersey.server.ResourceConfig; 
 
@ApplicationPath("services") 
public class CourseMgmtRESTApplication extends ResourceConfig { 
 
  public CourseMgmtRESTApplication () { 
    packages("packt.jee.eclipse.rest.ws.services"); 
  } 
 
} 

We have used the @ApplicationPath annotation to specify URL mapping for REST services implemented using JAX-RS. All @Path URIs on resource implementation classes will be relative to this path. For example, the "/course" URI that we specified for the CourseService class would be relative to "services", specified in the @ApplicationPath annotation.

Before we deploy the application and test our service, we need to generate web.xml. Right-click on the project in Project Explorer and select Java EE Tools | Generate Deployment Descriptor Stub. This will create web.xml in the WEB-INF folder. We don't need to modify it for this example.

Configure Tomcat in Eclipse as described in the Installing Tomcat section of Chapter 1, Introducing JEE and Eclipse, and in the Configuring Tomcat in Eclipse section of Chapter 2, Creating a Simple JEE Web Application. To deploy the web application, right-click on the configured Tomcat server in the Servers view and select the Add and Remove option. Add the current project.

Start the Tomcat server by right-clicking on the configured server in the Servers view and selecting Start.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43