简体   繁体   English

找不到Java类java.util.ArrayList和Java类型类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器

[英]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: 以下是我的环境的设置:

  • Tomcat 8, 雄猫8
  • Jersey libraries: jersey-bundle-1.19.1, 球衣库:jersey-bundle-1.19.1,
  • Not using maven 不使用Maven

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 我也尝试使用

  • jersey-core-1.8.jar 球衣核心 - 1.8.jar
  • jersey-json-1.8.jar 新泽西州JSON-1.8.jar
  • jersey-server-1.8.jar 新泽西服务器1.8.jar

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.

相关问题 严重:未找到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器 - SEVERE: A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found 未找到Java类java.util.ArrayList ...和MIME媒体类型text / xml的消息正文编写器 - A message body writer for Java class java.util.ArrayList…and MIME media type text/xml was not found Java类java.util.ArrayList的消息体编写器 - A message body writer for Java class java.util.ArrayList 找不到媒体类型=应用程序/json,类型=类 java.util.ArrayList 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList 未找到类型为 java.util.ArrayList 的返回值的转换器 - No converter found for return value of type: class java.util.ArrayList 我看到错误:Java类java.util.ArrayList和Java类型java.util.List的消息正文编写器<java.lang.String> - I am seeing the error: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<java.lang.String> 获取错误Java类java.util.ArrayList / List的消息正文编写器 <java.lang.String> 没有找到 - Getting error A message body writer for Java class java.util.ArrayList/List<java.lang.String> was not found 未找到 Media type=text/plain、type=class java.util.ArrayList、genericType=java.util.List 的 MessageBodyWriter<models.Person> - MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List<models.Person>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM