JAX-RS uses Java annotations to map an incoming HTTP request to a Java method. The process for mapping HTTP Requests/Responses will is a function of the MediaTypes designated by the consumes/produces.
For more information on annotations and mapping, see Mapping Incoming HTTP Requests to Java Methods, in Developing and Securing RESTful Web Services for Oracle WebLogic Server.
A request method designator annotations are runtime annotations, defined by JAX-RS, and which correspond to the similarly named HTTP methods. Within a resource class file, HTTP methods are mapped to Java programming language methods using the request method designator annotations. The behavior of a resource is determined by which of the HTTP methods the resource is responding to. Jersey defines a set of request method designators for the common HTTP methods: @GET, @POST, @PUT, @DELETE, @HEAD.
To map an HTTP Request to a Java method in the REST wizard
These steps assume that you have a JPA faceted project with generated entities. You will also need to have the JAX-RS facet enabled. (See Creating Projects Configured for REST.)
Select File>New>Other>Web Services> Rest Web Services.
Selecct Entities. The HTTP Type is designated given the following rules:
PUT- method to create or update a storage container.
GET - Requests data from a specified resource.
POST-Submits data to be processed to a specified resource.
DELETE-Deletes the specified resource.
HEAD-Same as GET but returns only HTTP headers and no document body.
Open your POJO in the source editor.
Open the REST Annotations View.
Place your cursor on the Class Type and Specify the @Path Annotation, making this a JAX-RS.
Place your cursor on the given method and add the appropriate JAX-RS request method designator annotation as defined by the HTTP type rules.
The javax.ws.rs.GET annotation transmits a representation of the resource identified by the URI to the client. The format or the representation returned in the response entity body cant be HTML, plain text, JPEG, or another form. Example: Mapping the HTTP GET Request to a Java Method shows how to map an HTTP GET Request to a Java method in a class called BookmarksResource.
Mapping the HTTP GET Request to a Java Method
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
...
public class BookmarksResource {
...
@Path("{bmid: .+}")
public BookmarkResource getBookmark(@PathParam("bmid") String bmid) {
return new BookmarkResource(uriInfo, em,
userResource.getUserEntity(), bmid);
}
@GET
@Produces("application/json")
public JSONArray getBookmarksAsJsonArray() {
JSONArray uriArray = new JSONArray();
for (BookmarkEntity bookmarkEntity : getBookmarks()) {
UriBuilder ub = uriInfo.getAbsolutePathBuilder();
URI bookmarkUri = ub .
path(bookmarkEntity.getBookmarkEntityPK().getBmid()).
build();
uriArray.put(bookmarkUri.toASCIIString());
}
return uriArray;
}
...
}
Use the Annotations view to add your request and response requests to your POJOs.
To create REST web services using the Annotation view
Create a POJO.
Open the Annotations view.
Put cursor on the type declaration, this will orient the available annotations to this location.
For the request customization, add the @Consumes annotation and specify the value. This sets the value of the media type for that annotation. Note that when @Consumes is applied at the method level, it overrides any @Consumes annotations applied at the class level.
For the response customization select the @Produces annotation in the properties view. Select the appropriate media type for your response. Note: If it is applied at the method level, it overrides any @Produces annotations applied at the class level.