[英]A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found
I am new to using jersy for implementing rest api 我是刚开始使用jersy实现rest api
I get the below error, when I call the products service. 致电产品服务时,出现以下错误。
com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found.
Here is my code: 这是我的代码:
@GET
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
public Response productSearch(@QueryParam("name") String name)
{
List<Product> products = new ArrayList<>();
products.add(new Product("PRDNAME", "PRDCOST", "PRDMODEL"));
return Response.ok( products).build();
}
I also tried this: 我也试过这个:
@GET
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
public List<Product> productSearch(@QueryParam("name") String name)
{
List<Product> products = new ArrayList<>();
products.add(new Product("PRDNAME", "PRDCOST", "PRDMODEL"));
return products;
}
The below is the setup of my environment: 以下是我的环境的设置:
web.xml: web.xml中:
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.test.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I tried using the below init-param also: 我也尝试使用以下初始化参数:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
I tried returning GenericEntity, but had the same issue. 我尝试返回GenericEntity,但是遇到了同样的问题。
return Response.ok().entity(new GenericEntity<List<Product>>(products) {}).build();
I also tried using 我也尝试使用
instead of jersey-bundle-1.19.1 而不是jersey-bundle-1.19.1
I don't have any issues when I return string. 返回字符串时,我没有任何问题。 I understand, I am missing the json media type dependency, but could not figure it out. 我了解,我缺少json媒体类型依赖项,但无法弄清楚。
This error happens on the tomcat server while the client doesn't receive response obviously. 客户端未明显收到响应时,此错误在tomcat服务器上发生。
Maybe you forgot to add 也许您忘记添加
@XmlRootElement
annotation on top of model class definition. 在模型类定义之上的注释。 For your "Product" model, I guess it would be something like 对于您的“产品”模型,我想这就像
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Product{
....
}
I had a similar exception and this solved it for me. 我有一个类似的例外,这为我解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.