Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward a request using JAX-RS?

I want to forward a REST request to another server.

I use JAX-RS with Jersey and Tomcat. I tried it with setting the See Other response and adding a Location header, but it's not real forward.

If I use:

request.getRequestDispatcher(url).forward(request, response); 

I get:

  • java.lang.StackOverflowError: If the url is a relative path
  • java.lang.IllegalArgumentException: Path http://website.com does not start with a / character (I think the forward is only legal in the same servlet context).

How can I forward a request?

like image 299
user2313423 Avatar asked Jul 15 '13 12:07

user2313423


People also ask

What is the difference between JAX-RS and Jersey?

JAX-RS is a specification (which basically tells what to implement/follow) and Jersey is an implementation (which means how those specifications should be implemented). We can have multiple implementations for a Specification.

What is the use of JAX-RS?

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.

What are the JAX-RS implementations?

JAX-RS uses annotations, introduced in Java SE 5, to simplify the development and deployment of web service clients and endpoints. From version 1.1 on, JAX-RS is an official part of Java EE 6. A notable feature of being an official part of Java EE is that no configuration is necessary to start using JAX-RS.

What does JAX-RS stand for?

INTRODUCTION. This article introduces you to the Java API for RESTful Web Services (JAX-RS), which resulted from Java Specification Request (JSR) 311 and is a component of the Java Enterprise Edition Platform (Java EE 6).


1 Answers

Forward

The RequestDispatcher allows you to forward a request from a servlet to another resource on the same server. See this answer for more details.

You can use the JAX-RS Client API and make your resource class play as a proxy to forward a request to a remote server:

@Path("/foo")
public class FooResource {

    private Client client;

    @PostConstruct
    public void init() {
        this.client = ClientBuilder.newClient();
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response myMethod() {

        String entity = client.target("http://example.org")
                              .path("foo").request()
                              .post(Entity.json(null), String.class);   

        return Response.ok(entity).build();
    }

    @PreDestroy
    public void destroy() {
        this.client.close();
    }
}

Redirect

If a redirect suits you, you can use the Response API:

  • Response.seeOther(URI): Used in the redirect-after-POST (aka POST/redirect/GET) pattern.
  • Response.temporaryRedirect(URI): Used for temporary redirection.

See the example:

@Path("/foo")
public class FooResource {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response myMethod() {

        URI uri = // Create your URI
        return Response.temporaryRedirect(uri).build();
    }
}

It may be worth it to mention that UriInfo can be injected in your resource classes or methods to get some useful information, such as the base URI and the absolute path of the request.

@Context
UriInfo uriInfo;
like image 188
cassiomolin Avatar answered Sep 28 '22 03:09

cassiomolin