【问题标题】:Cannot have JSP page load with Jersey server无法使用 Jersey 服务器加载 JSP 页面
【发布时间】:2013-11-08 18:10:42
【问题描述】:

我将 jersey 2.4 用于我的 Web 服务,并且无法让主页加载 index.jsp。我也做了一个 IndexService POJO 来尝试从那里加载它,但这不起作用。我想只使用主页,而不是使用 IndexService POJO。到达 POJO 但返回:

HTTP 状态 500 - org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型 = 文本/html、类型 = 类 org.glassfish.jersey.server.mvc.Viewable、通用类型 = 类组织的 MessageBodyWriter。 glassfish.jersey.server.mvc.Viewable。

我的 web.xml 文件:

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

依赖 pom.xml 文件:

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

索引服务 POJO:

@Path("/")
public class IndexService {

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

【问题讨论】:

    标签: jsp jersey jersey-2.0


    【解决方案1】:

    您需要将JspMvcFeature 添加到应用程序的配置中。由于您使用web.xml 来配置您的应用程序(资源和提供程序的包扫描,设置属性),您需要添加以下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 的回答是正确的,但我还要添加有关通过扩展 JAX-RS Application 类或 Jersey ResourceConfig 类来完成配置的情况的详细信息。

    请注意,从 Jersey 2.x 开始,您必须明确注册要使用的扩展功能 (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;
        }
    
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2015-04-14
      • 1970-01-01
      • 2013-09-17
      • 2011-08-06
      • 1970-01-01
      • 2019-08-27
      • 2015-04-05
      • 1970-01-01
      相关资源
      最近更新 更多