jmm
jmm

Reputation: 1064

Getting 404 trying to render a JSP from Jersey 2

I'm trying to make a simple example of a web application using Jersey 2 and JSP, and to deploy it on Glassfish 4. However, I keep getting 404 errors trying to access my only resource.

First, the project structure:

Project structure

Here are the important classes and files:

web.xml I'm setting Jersey as a filter, instead of a servlet, because I read it needs to be defined that way in order to make it work with JSP.

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name>Archetype Created Web Application</display-name>

  <filter>
    <filter-name>jersey</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.xxx.sample-jersey2</param-value>
    </init-param>
    <init-param>
      <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

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

  <!--<welcome-file-list>-->
    <!--<welcome-file>index.html</welcome-file>-->
    <!--<welcome-file>index.htm</welcome-file>-->
    <!--<welcome-file>movies.jsp</welcome-file>-->
  <!--</welcome-file-list>-->

</web-app>

movies.jsp is extremely simple:

<html>
<body>
  <h2>Listing Movies</h2>
</body>
</html>

The custom ResourceConfig (which I'm not even sure I need)

package com.xxx.jersey2.config;

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;

public class CustomResourceConfig extends ResourceConfig {
    public CustomResourceConfig() {
        packages("com.xxx");
        register(LoggingFilter.class);
        register(JspMvcFeature.class);
        // it may be also jersey.config.server.mvc.templateBasePath
        property("jersey.config.server.mvc.templateBasePath", "/WEB-INF/jsp");
        // ... more properties as needed ...
    }
}

And my controller for the Movies:

package com.xxx.jersey2.web;

import com.xxx.jersey2.model.Movie;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.glassfish.jersey.server.mvc.Viewable;

@Path("movies")
public class MoviesController {
    @GET
    @Path("/list")
    public Viewable getMovies() {
        List<Movie> movies = new ArrayList<Movie>();

        movies.add(new Movie("Million Dollar Baby", "Hillary Swank"));
        movies.add(new Movie("Toy Story", "Buzz Light Year"));
        movies.add(new Movie("Hunger Games", "Jennifer Lawrence"));

        return new Viewable("movies.jsp");
    }
}

If I try to access https://localhost:8181/sample-jersey2/ the simple index.jsp is rendered (which only shows a Hello World message). However, trying to access https://localhost:8181/sample-jersey2/movies/list/ gives me a 404 error.

Any ideas on this would be greatly appreciated. Best regards

Upvotes: 2

Views: 1950

Answers (1)

s7vr
s7vr

Reputation: 75964

You're in right direction. Please register your custom resource config in web.xml.

     <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.xxx.jersey2.config.CustomResourceConfig</param-value>
    </init-param>

You can remove the init param jersey.config.server.provider.packages as you're already doing that in your resource config. One last thing change the return new Viewable("movies.jsp") to new Viewable("/movies.jsp"). Hopefully it will work.

Upvotes: 2

Related Questions