Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Developers can use POJOs to define a resource. Root resources have a @Path annotation at the class declaration level. JAX-RS matches an incoming request's URL with the @Path annotation on all of an application's root resources to determine which initial Java class will handle the request.

Root resources Java class instances are created per request by default. See the JAX-RS Application configuration topic for more information.

Resource classes have methods that are invoked when specific HTTP method requests are made, referred to as resource methods. To create Java methods that will be invoked with specific HTTP methods, implement a regular Java method and annotate it with one of the JAX-RS @HttpMethod annotated annotations (namely, @GET, @POST, @PUT, and @DELETE).

...

Any incoming GET request that has the URL of "/welcome" would be handled by WelcomeMessage class's returnWelcomeMessage() method. A string is returned which represents the response body . To return more complex response entities, see and is sent as the response payload in a HTTP 200 status response.

javax.ws.rs.core.Response

In the above GET resource method, the response body is returned as a String. If a more complex response is required (i.e. additional HTTP response headers, different status code, etc.), a javax.ws.rs.core.Response should be used as the Java method's return type. By building a Response object, additional information can be returned to the client.

Code Block

@Path("/welcome")
public class WelcomeMessage {
    private String welcomeMessage = "Hello world!";

    @GET
    public Response returnWelcomeMessage() {
        String responseEntity = welcomeMessage;
        return Response.status(299).entity(responseEntity).header("CustomHeader", "CustomValue").build();
    }
}

While the above example uses 299 as the status code, standard HTTP status codes should be used to help clients understand responses. Also, while Strings have been used as the response entity, different Java types may be used for complex responses. See the Request/Response entities page for more details on how request/response entities are handled.

Anchor
HTTPMethods
HTTPMethods

...