我有以下代码: @GET @Path("v1/entity") @ApiOperation( value = "List", notes = "Enables you to List.", tags = { "List" }) @ApiImplicitParams( { @ApiImplicitParam(name = "pageSize", value = "Page Size", dataType = "int", paramType = "formData", example = "50"), @ApiImplicitParam(name = "sortAscending", value = "Sort Ascending", dataType = "boolean", paramType = "formData", example = "false") }) public Response list(@ApiParam(hidden = true) Integer pageSize, @ApiParam(hidden = true) Boolean sortAscending) { Collection<EntityData> dataCollection; if (pageSize == null || sortAscending == null || pageSize <= 0) { dataCollection = storeController.list(); } else { SortDirection sortDirection = sortAscending ? SortDirection.ASC : SortDirection.DESC; dataCollection= storeController.list(pageSize, sortDirection); } logger.info("List contains {} elements", dataCollection.size()); GenericEntity<Collection<EntityData>> response = new GenericEntity<Collection<EntityData>>(dataCollection){}; return Response.ok(response).build(); //return ResponseEntity.ok(dataCollection); }当我调用 API 时,出现以下错误:No message body writer has been found for class java.util.ArrayList, ContentType: */*我也尝试过使用 来Response检索内容,而不是使用ResponseEntity<Collection<EntityData>>,但错误仍然存在。ArrayList就像直接使用中的“ ”一样Response(不换行GenericEntity)。如果我将最后一行更改为return Response.ok().build();它可以工作,但我需要Collection检索...logger表明当我运行时集合有 9 个元素。我知道我在这里错过了一些东西,但我看不到它。你能帮助我吗?
1 回答
![?](http://img1.sycdn.imooc.com/54585094000184e602200220-100-100.jpg)
墨色风雨
TA贡献1853条经验 获得超6个赞
确保您的资源方法(或整个资源类)使用以下注释:
@Produces("applicaton/json")
或者;@Produces(MediaType.APPLICATION_JSON)
.
然后确保类路径中有一个 JSON 提供程序,例如 Jackson:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version>
</dependency>
根据 JAX-RS 实现的配置方式,将jackson-jaxrs-json-provider
工件添加到类路径就足以使用 Jackson 作为 JSON 提供程序。否则您需要注册JacksonJsonProvider
。
添加回答
举报
0/150
提交
取消