簡體   English   中英

以集合為實體的javax.ws.rs.core.Response

[英]javax.ws.rs.core.Response with a collection as entity

我有以下代碼:

    @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: */*

以此為參考 ,但是沒有用。 我也嘗試過使用ResponseEntity<Collection<EntityData>> ,而不是使用Response來檢索內容,但是錯誤仍然存​​在。 就像在Response直接使用“ ArrayList ”一樣(不包裝GenericEntity )。

如果我更改最后一行以return Response.ok().build(); 它可以工作,但是我需要檢索Collectionlogger指示我運行時Collection具有9個元素。

我知道我在這里遺漏了一些東西,但是看不到。 你能幫助我嗎?

確保使用以下方法對資源方法(或整個資源類)進行注釋:

  • @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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM