Cannot have JSP page load with Jersey server

半腔热情 提交于 2019-12-10 15:40:00

问题


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);
    }
}

回答1:


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>



回答2:


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;
    }

    // ...
}


来源:https://stackoverflow.com/questions/19865738/cannot-have-jsp-page-load-with-jersey-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!