我有以下代碼:? ? @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 回答

墨色風雨
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
提交
取消