问题描述
问题是,每当我尝试产生除JSON之外的其他任何媒体类型的响应时,都会收到"MessageBodyWriter"未找到错误.但是对于Application_JSON输出,响应正常.我在Maven POM中使用了多个依赖项,因此MessageBodyWriter可能会被覆盖,或者可能仅包含其中的JSON.
The problem is that whenever I try to produce response of media type anything other than JSON, I get "MessageBodyWriter" not found error.But the response works fine for Application_JSON outputs.I am using several dependencies in my Maven POM so maybe MessageBodyWriter is getting overridden or maybe only for JSON its included.
以下是pom依赖项-
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1003-jdbc4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!-- http://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<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.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc</artifactId>
<version>2.5.1</version>
</dependency>
<!-- http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl </artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
我正在使用的示例Java类是-
The sample Java class I am using is -
package com.va;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.server.mvc.Viewable;
@Path("orders")
public class OrderView{
@GET
@Produces(MediaType.TEXT_HTML)
public Viewable index() {
return new Viewable("index");
}
}
我也将index.htm文件保存在webapp文件夹中.web.xml的配置为-
Also I have kept my index.htm file in webapp folder.The config for web.xml is as -
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.va</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/webapp</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/(webapp)/.*</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
在访问URL localhost:8080/myapp/orders时我收到HTTP 500错误和控制台错误
On accessing the URL localhost:8080/myapp/ordersI get HTTP 500 error and console error as
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/html, type=class org.glassfish.jersey.server.mvc.Viewable, genericType=class org.glassfish.jersey.server.mvc.Viewable.
请指导我解决问题.提前谢谢.:)
Please guide me with what is the issue. Thanks in advance.:)
推荐答案
在这里需要注意的事情:在使用jersey mvc模板时,应该使用servlet过滤器而不是serlvet.另外,您的某些init参数引用Jersey 1,应更改为等效于Jersey 2. Jersey MVC模板
Couple of things to note here you should be using servlet filters not serlvet when using jersey mvc template. Also, some of your init parameter referring to Jersey 1 and should be changed to Jersey 2 equivalent. More here Jersey MVC Template
更新了web.xml.试试看.
Updated web.xml. Give it a try.
<filter>
<filter-name>Jersey REST Service</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.va</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.mvc.templateBasePath</param-name>
<param-value>/webapp</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
</init-param>
<init-param>
<param-name>jersey.config.servlet.filter.forwardOn404</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</filter>
<filter-mapping>
<filter-name>Jersey REST Service</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这篇关于找不到针对媒体类型= text/html的MessageBodyWriter,但适用于JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!