user1530580
user1530580

Reputation: 189

Cannot have JSP page load with Jersey server

I am using jersey 2.4 for my web service and cannot have the home page load the index.jsp. I made a IndexService POJO to try loading it from there too, but that doesn't work. I would like to just use the home page, instead of the having an IndexService POJO. The POJO is reached but returns this:

HTTP Status 500 - org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class org.glassfish.jersey.server.mvc.Viewable, genericType=class org.glassfish.jersey.server.mvc.Viewable.

My web.xml file:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>rest</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name>
        <param-value>/WEB-INF/jsp/</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Dependencies pom.xml files:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.4</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-mvc</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-mvc-jsp</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>

    <dependency>
        <groupId>xom</groupId>
        <artifactId>xom</artifactId>
        <version>1.2.5</version>
    </dependency>
<dependencies>

IndexService POJO:

@Path("/")
public class IndexService {

    @GET
    @Path("/index")
    @Produces(MediaType.TEXT_HTML)
    public Viewable indexPage() {
        return new Viewable("/index.jsp", null);
    }
}

Upvotes: 4

Views: 5245

Answers (2)

xonya
xonya

Reputation: 2484

Michal Gajdos answer is correct, but I would also to add details about the cases in which the configuration is done by extending the JAX-RS Application class or the Jersey ResourceConfig class.

Notice that from Jersey 2.x you have to explicitly register the extension Features you are going to use (see official documentation).

@javax.ws.rs.ApplicationPath("/rest")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        resources.add(org.glassfish.jersey.server.mvc.jsp.JspMvcFeature.class);
        return resources;
    }

    // ...
}

Upvotes: 4

Michal Gajdos
Michal Gajdos

Reputation: 10379

You need to add JspMvcFeature to the configuration of your application. Since you're using web.xml to configure your application (package scanning of resources and providers, setting property) you need to add the following init-parameter:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
</init-param>

Upvotes: 12

Related Questions