creating RESTful Web Services


creating RESTful Web Services

JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture.The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.Java programming language class files comes with JAX-RS annotations to define resources and the actions that can be performed on those resources. JAX-RS annotations are runtime annotations; therefore, runtime reflection will generate the helper classes and artifacts for the resource.
restful-web-services-rest
The following code sample is a very simple example of java class that uses JAX-RS annotations:
package com.sun.java.samples.helloworld.resources; 

import javax.ws.rs.GET;

import javax.ws.rs.Produces;

import javax.ws.rs.Path;

// The Java class will be hosted at the URI path "/helloworld"

@Path("/helloworld")

public class HelloWorldResource {

// The Java method will process HTTP GET requests

@GET

// The Java method will produce content identified by the MIME Media

// type "text/plain"

@Produces("text/plain")

public String getClichedMessage() {

// Return some cliched textual content

return "Hello World";

}

}
The following sections describe the annotations used in this example.
The @Path annotation’s value is a relative URI path. In the preceding example, the Java class will be hosted at the URI path /helloworld. This is an extremely simple use of the @Path annotation, with a static URI path. Variables can be embedded in the URIs. URI path templates are URIs with variables embedded within the URI syntax.

The @GET annotation is a request method designator, along with @POST, @PUT, @DELETE, and @HEAD, defined by JAX-RS and corresponding to the similarly named HTTP methods. In the example, the annotated Java method will process HTTP GET requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

The @Produces annotation is used to specify the MIME media types a resource can produce and send back to the client. In this example, the Java method will produce representations identified by the MIME media type "text/plain".

The @Consumes annotation is used to specify the MIME media types a resource can consume that were sent by the client. The example could be modified to set the message returned by the getClichedMessage method, as shown in this code example:

@POST

@Consumes("text/plain")

public void postClichedMessage(String message) {

// Store the message

}

0 comments:

Post a Comment