1

I have this problem. I'm trying to use Jersey 2.x in order to make REST api, and I would like to use jsp pages for my template (everything without spring or springboot, I can't include them into my project). I have a maven project with this structure:

enter image description here

the pom.xml has the following dependencies:

<dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-client</artifactId>
      <version>2.27</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-common</artifactId>
      <version>2.27</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-server</artifactId>
      <version>2.27</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.27</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
      <version>2.27</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.27</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-mvc-jsp</artifactId>
      <version>2.27</version>
    </dependency>
  </dependencies>

and the web.xml is:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
        <servlet-name>testjsp</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>it.testjsp.config.JerseyConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>testjsp</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

When I run my app with tomcat, the following url http://localhost/testjsp

redirects correctly the index.hml.

I have configured the JerseyConfig class in the following way:

@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("it.testjsp.endpoints");

        register(JspMvcFeature.class);

        property("jersey.config.server.mvc.templateBasePath", "/WEB-INF/jsp");
    }
}

In order to map all the end points exposed into that package and to use jsp pages into WEB-INF/JSP.

I have two end points:

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public class TestEndPoint {

    @GET
    public Map<String, Object> testApi() {
        System.out.println("test jersey 2.27");
        Map<String, Object> result = new HashMap<String, Object>();

        result.put("result", "test jersey 2.27");
        return result;
    }
}

This is inside TestEndPoint class and the application responds as I expect (http://localhost/testjsp/api/test returns the json).

The PagesEndPoint is:

@Path("/pages")
public class PagesEndPoint {

    @GET
    @Path("{pageName}")
    public Viewable getPage(@PathParam("pageName") String pageName) {
        System.out.println("Try " + pageName + ".html");
        return new Viewable("/" + pageName + ".html");
    }

}

But when I run the app with tomcat, I have always a 404. Is possible to use jsp (or other html pages)? What I did wrong? Thanks for the help

d_vucin91
  • 119
  • 2
  • 12
  • 1
    From what I remember, when using JSP, I think you need to configure Jersey as a servlet filter, not a servlet. [See example](https://stackoverflow.com/a/31900846/2587435) – Paul Samsotha Sep 22 '18 at 19:56
  • @PaulSamsotha thanks for your help. I've changed the web.xml as described into the other post, then I've removed the **ApplicationPath** tag on the JerseyConfig method, changed the `@Path("/pages/)` into `@Pages("${pageName}` and works for me. Thanks – d_vucin91 Sep 26 '18 at 10:31

0 Answers0