[英]javax.ws.rs.core.Response with a collection as entity
I have the following code: 我有以下代码:
@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);
}
Both methods from storeController
object only return an ArrayList
of EntityData, whose structure is below: storeController
对象的两个方法仅返回EntityData的ArrayList
,其结构如下:
public class EntityData implements Serializable {
private String document;
private String validTo;
private String validFrom;
private String entityAlias;
public String getDocument() {
return document;
}
public void setDocument(String document) {
this.document= document;
}
public String getValidTo() {
return validTo;
}
public void setValidTo(String validTo) {
this.validTo = validTo;
}
public String getValidFrom() {
return validFrom;
}
public void setValidFrom(String validFrom) {
this.validFrom = validFrom;
}
public String getEntityAlias() {
return entityAlias;
}
public void setEntityAlias(String entityAlias) {
this.entityAlias = entityAlias;
}
}
When I call the API, I get this error: 当我调用API时,出现以下错误:
No message body writer has been found for class java.util.ArrayList, ContentType: */*
I used this as a reference , but it didn't work. 我以此为参考 ,但是没有用。 I've also tried to, instead of using
Response
to retrieve the contents, using ResponseEntity<Collection<EntityData>>
, but the error persists. 我也尝试过使用
ResponseEntity<Collection<EntityData>>
,而不是使用Response
来检索内容,但是错误仍然存在。 Just as using directly the " ArrayList
" in the Response
(without wrapping on GenericEntity
). 就像在
Response
直接使用“ ArrayList
”一样(不包装GenericEntity
)。
If I change the last line to return Response.ok().build();
如果我更改最后一行以
return Response.ok().build();
it works, but I need the Collection
to be retrieved... The logger
indicates that the Collection has 9 elements when I run. 它可以工作,但是我需要检索
Collection
。 logger
指示我运行时Collection具有9个元素。
I know I am missing something here, but I can't see it. 我知道我在这里遗漏了一些东西,但是看不到。 Can you help me?
你能帮助我吗?
Make sure your resource method (or the entire resource class) is annotated either with: 确保使用以下方法对资源方法(或整个资源类)进行注释:
@Produces("applicaton/json")
or; @Produces("applicaton/json")
或; @Produces(MediaType.APPLICATION_JSON)
. @Produces(MediaType.APPLICATION_JSON)
。 Then ensure you have a JSON provider, such as Jackson, in the classpath: 然后确保在类路径中有一个JSON提供程序,例如Jackson。
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version>
</dependency>
Depending on how the JAX-RS implementation is configured, adding the jackson-jaxrs-json-provider
artifact to your classpath is enough to use Jackson as a JSON provider. 根据JAX-RS实现的配置方式,将
jackson-jaxrs-json-provider
工件添加到您的类路径中就足以使用Jackson作为JSON提供程序。 Otherwise you need to register JacksonJsonProvider
. 否则,您需要注册
JacksonJsonProvider
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.