6

I've been writing a prototype Jersey (JAX-RS) application and wanted to try handling application/x-www-form-urlencoded posts with a redirect-after-POST methodology.

I want to redirect to an html page hosted at the application root on success, however I can't seem to escape out of Jersey's servlet root.

Here's an example of a resource which allows you to create a new user:

URI I want: /jersey-test/user.html

URI I get: /jersey-test/r/user.html

@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public Response putUser(@Context UriInfo uriInfo, 
    MultivaluedMap<String, String> formParams) {

    // snip... do work and insert user here...

    URI uri = uriInfo.getBaseUriBuilder().path("user.html").build();
    return Response.seeOther(uri).build();
}

Relevant snippets from my web.xml:

<web-app ...>
  <display-name>jersey-test</display-name>
  ...
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    ...
  </servlet>
  ...
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/r/*</url-pattern>
  </servlet-mapping>
</web-app>
JavadocMD
  • 4,397
  • 2
  • 25
  • 23

1 Answers1

8

Assign the path like this:

URI uri = uriInfo.getBaseUriBuilder().path("../user.html").build();
Valeh Hajiyev
  • 3,216
  • 4
  • 19
  • 28
  • Oh man. That was too easy. Works like a charm though, thanks! – JavadocMD Mar 01 '13 at 01:09
  • Would you mind taking a look at my question: http://stackoverflow.com/questions/35123194/jersey-2-render-swagger-static-content-correctly-without-trailing-slash, you answer here doesn't work for my case – macemers Feb 16 '16 at 10:08
  • there is one good example for this http://www.javaproficiency.com/2015/04/redirect-in-jersey.html – Anuj Dhiman Aug 15 '16 at 10:47