craig
craig

Reputation: 26262

JAX-RS: Path that ends in slash (/) causes a 404 error

Attempts to reach a resource at http://localhost:8080/enterprise/session/new/ result in a HTTP 404 (not found) error. Attempts to reach the same resource at http://localhost:8080/enterprise/session/new work as expected.

Session Resource:

@GET
@Path("/new")
@Produces(MediaType.TEXT_HTML)
public Viewable newSession() {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("method","secEnterprise");

    // create authentication form
    return new Viewable("/session/new", map);

}

View is located at WEB-INF\views\session\new.jsp

I suspect that part of the problem is related to the section of the web.xml:

<filter>
    <filter-name>jersey</filter-name>
    <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.businessobjects.resources</param-value>
    </init-param>        
    <init-param>
        <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
        <!-- is this capturing the '/' and redirecting it to a non-existent view (.JSP)? -->
        <param-value>/((WEB-INF/views))/.*</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
        <param-value>/WEB-INF/views/</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.feature.Redirect</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
        <param-value>true</param-value>
    </init-param>        
</filter>

<filter-mapping>
    <filter-name>jersey</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I'm not sure if this is related, but the view's form POSTs to /session/session, rather than just to /session:

<!-- new.jsp -->
...
<form method="post" action="session">
...

What am I missing?

** edit 0 **

Changing the action attribute of the form element to

<form method="post" action="/enterprise/session">

works as expected. However, an ending slash:

<form method="post" action="/enterprise/session/">

results in the same error mentioned earlier.

It seems like the regex expression /((WEB-INF/views))/.* needs to be changed to ignore ending slashes ('/').

** edit 1 **

I have a root resource:

@Path("/")
public class HomeResource {

    @GET
    public Response index() {
        return Response.ok(new Viewable("/home/index")).build();
    }
}

Requests to http://localhost:8080/enterprise are automatically routed to http://localhost:8080/enterprise/, so some aspects of the automatic redirection are working correctly.

Upvotes: 2

Views: 1874

Answers (1)

Vishal Biyani
Vishal Biyani

Reputation: 4407

Not sure which implementation you are using but quoting from Jersey documentation:

A @Path value may or may not begin with a '/', it makes no difference. Likewise, by default, a @Path value may or may not end in a '/', it makes no difference, and thus request URLs that end or do not end in a '/' will both be matched. However, Jersey has a redirection mechanism, which if enabled, automatically performs redirection to a request URL ending in a '/' if a request URL does not end in a '/' and the matching @Path does end in a '/'.

Upvotes: 1

Related Questions