# Mapping Incoming Requests to Java Methods

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.

## How to Map an HTTP Request to Java Methods in the REST Generation Wizard

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.)

1. Select File>New>Other>Web Services> Rest Web Services.

2. 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.

3. Open your POJO in the source editor.

4. Open the REST Annotations View.

5. Place your cursor on the Class Type and Specify the @Path Annotation, making this a JAX-RS.

6. Place your cursor on the given method and add the appropriate JAX-RS request method designator annotation as defined by the HTTP type rules.

## How to Map HTTP Requests to Java Methods in the Java Class

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;
    }
    ...
    }

## How to Map HTTP Requests to Java Methods in the Annotations View

Use the Annotations view to add your request and response requests to your POJOs.

To create REST web services using the Annotation view

1. Create a POJO.

2. Open the Annotations view.

3. Put cursor on the type declaration, this will orient the available annotations to this location.

4. 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.