我正在使用针对我的网络服务的jersey 2.4并且不能让主页加载index.jsp。我做了一个IndexService POJO尝试从那里加载它,但这不起作用。我想只使用主页,而不是使用IndexService POJO。到达POJO但返回:
HTTP状态500 - org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= text / html的MessageBodyWriter,类型= class org.glassfish.jersey.server.mvc.Viewable,genericType = class org。 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>
IndexService POJO:
@Path("/")
public class IndexService {
@GET
@Path("/index")
@Produces(MediaType.TEXT_HTML)
public Viewable indexPage() {
return new Viewable("/index.jsp", null);
}
}
答案 0 :(得分:11)
您需要将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>
答案 1 :(得分:3)
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;
}
// ...
}