简体   繁体   English

返回列表时“找不到MessageBodyWriter” <Long> 与JAX-RS中的响应对象

[英]“MessageBodyWriter not found” when returning List<Long> with the Response-object in JAX-RS

I am currently trying to create a rest-webservice which returns an object of the type List< Long> . 我当前正在尝试创建一个rest-webservice,该服务返回类型为List <Long>的对象。 The service is annotated with @Produces(MediaType.APPLICATION_JSON) . 该服务使用@Produces(MediaType.APPLICATION_JSON)进行注释。

I don't have any problems with the other services in this project. 我对该项目的其他服务没有任何问题。

If I write the code as following: 如果我将代码编写如下:

List<Long> list = new ArrayList<Long>();
            list.add(100L);
            list.add(200L);
            list.add(300L);

return Response.ok(list)
               .build();

Then I get the following message in the log: 然后,我在日志中收到以下消息:

MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.

If I instead write it as following (same as I did for another service in the project, but where it is a List of pojo objects): 如果我改写如下(与我在项目中为另一项服务所做的相同,但是它是pojo对象的列表):

List<Long> list = new ArrayList<Long>();
            list.add(100L);
            list.add(200L);
            list.add(300L);

GenericEntity<List<Long>> genericEntity = new GenericEntity<List<Long>>(list){};

return Response.ok(genericEntity)
               .build();

Then I get the following message instead: 然后我得到以下消息:

MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<java.lang.Long>.

After googling I found the following suggestion to convert the List to an array as seen below, but this didn't work either. 谷歌搜索后,我发现以下建议将List转换为数组,如下所示,但这也不起作用。

List<Long> list = new ArrayList<Long>();
            list.add(100L);
            list.add(200L);
            list.add(300L);

            return Response.ok(list.toArray()).build();

Then I get: 然后我得到:

MessageBodyWriter not found for media type=application/json, type=class [Ljava.lang.Object;, genericType=class [Ljava.lang.Object;.

We are using jackson-core version 2.3.2 and the jax-rs implementation is Jersey which is included with Weblogic 12c. 我们使用的是jackson-core版本2.3.2,而jax-rs实现是Jersey,Weblogic 12c附带了该实现。

Is there any good solution for this (without using any other libraries)? 有什么好的解决方案(不使用任何其他库)吗?

As you are producing json type data, Json related jar is required for parsing Object in to json form. 在生成json类型数据时,需要将Json相关的jar解析为json形式的Object。

Add Jackson jar to your pom.xml if it is Maven project. 如果它是Maven项目,则将Jackson罐添加到您的pom.xml中。

add this dependency.. 添加此依赖项。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.6</version>
</dependency>

Or 要么

download the jar and add it to class path. 下载jar并将其添加到类路径。

You did not mention which JAX-RS framework you're playing with, but according to Apache CXF documentation for instance, if you want to use Jackson as JSON Provider, you need this dependency: 您没有提到要使用哪个JAX-RS框架,但是例如,根据Apache CXF文档 ,如果要使用Jackson作为JSON Provider,则需要以下依赖关系:

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.9.3</version>
</dependency>

Then register one of the Jackson Providers on your JAX-RS service, I guess the one for POJOs in your case, not the one for JAXB beans (WARNING: the package name in CXF documentation is for Jackson 1.x, I replaced with the one for Jackson 2.x): 然后在您的JAX-RS服务上注册一个Jackson Providers,我猜这是针对您的POJO的,而不是JAXB bean的(警告:CXF文档中的软件包名称是针对Jackson 1.x的,我替换为一个用于Jackson 2.x):

<jaxrs:providers>
   <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
</jaxrs:providers>

For Jersey, it would be a different dependency . 对于泽西岛,那将是另一种依赖

While returning a List< Long> in a Response object doesn't work for me I have managed to come around it by embedding the list in a new class. 虽然在Response对象中返回List <Long>对我不起作用,但我设法通过将列表嵌入新类中来解决它。

The new class looks as following: 新类如下所示:

public class LongArrayList {

    private List<Long> list;

    public LongArrayList() {

    }

    public LongArrayList(List<Long> list) {
        this.setList(list);
    }

    public List<Long> getList() {
        return list;
    }

    public void setList(List<Long> list) {
        this.list = list;
    }
}

I then return this list as following: 然后,我返回此列表,如下所示:

return Response.ok(new LongArrayList(theOldLongList))
               .build();

Unfortunatelly this returns a little bigger json than I would want: 不幸的是,这会返回比我想要的大一点的json:

{"list":[5,12,29,30]}

If anybody finds a better solution I will accept it as an answer for this question instead. 如果有人找到更好的解决方案,我将接受它作为该问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM