javax.ws.rs.core.Response 以集合作为实体

问题描述 投票:0回答:2

我有以下代码:

    @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);
    }

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;
    }
}

当我调用 API 时,出现此错误:

No message body writer has been found for class java.util.ArrayList, ContentType: */*

我用了this作为参考,但是没有用。我也尝试过使用

Response
来检索内容,而不是使用
ResponseEntity<Collection<EntityData>>
来检索内容,但错误仍然存在。就像直接使用
ArrayList
中的“
Response
”一样(不包裹在
GenericEntity
上)。

如果我将最后一行更改为

return Response.ok().build();
它可以工作,但我需要检索
Collection
...
logger
表明我运行时集合有 9 个元素。

我知道我在这里遗漏了一些东西,但我看不到它。你能帮我吗?

java rest arraylist jax-rs response
2个回答
1
投票

确保您的资源方法(或整个资源类)使用以下注释:

  • @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
投票
// it is part of jerssy Glassfish
// when we have retuen type List<object>
// what is repsosne we return.
import jakarta.ws.rs.core.Response;

public List<EmpDTO>  smaple test()
{
        Response getempresposne = null;
List<EmpDTO> getemplist = new ArrayList<>();
         getemplist = getempresposne.readEntity(new GenericType<List<EmpDTO>>() {});

}
© www.soinside.com 2019 - 2024. All rights reserved.