durandal durandal - 2 months ago 8
Java Question

Jersey Viewable resulting in 404

I am attempting to create a RESTful web service in Java using Jersey and Tomcat. The problem I am having is that after authenticating a user, I try to return a Response with a Viewable containing the path to my file and I receive a 404 resource not found error.

I have tried every possible path I can think of, including adding multiple copies of map.jsp in the root, WEB-INF and in child folders of WEB-INF. I have also tried omitting the .jsp when returning the Response with the Viewable with no luck.

My folder structure looks like this:


  • Web Pages

    • map.jsp

    • META-INF

      • context.xml


    • WEB-INF

      • map.jsp

      • jsp

        • map.jsp

        • web.xml






The method below is called once authorization is complete. I have verified this by returning the String "hello world" which does display. Here is the code I currently have for my Resource class.

@Path("/")
public class WebServiceResource
{
private static final UserDao USER_DAO = new UserDao();
private static final Logger log = Logger.getLogger(WebServiceResource.class.getName());

@GET
@Produces(MediaType.TEXT_HTML)
public Response getJSP()
{
log.log(Level.ALL, "getJSP() method: ");
return Response.ok(new Viewable("map.jsp")).build();
}
}


Here is my web.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>my-webservice</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/jsp</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.company.mywebservice.ws.WebServiceApp</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;com.company.mywebservice.ws.Authorization</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/(resources|(WEB-INF))/.*</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>my-webservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Answer

For anyone experiencing this issue, here's the solution, courtesy of the link provided by peeskillet here. My issue was that I was missing the following dependency, though mine was a different version:

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>2.0-m07-1</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

After adding that dependency to my pom everything worked.