JAX-RS客户端返回null实体

San*_*ung 8 resteasy

我有一个像这样定义的JAX-RS服务:

@Produces(MediaType.APPLICATION_JSON)
@GET
@Path("/namestartswith")
public List<ProductBrand> nameStartsWith(@QueryParam("name") String name) {
    List<ProductBrand> productBrandList = productBrandService.findByNameStartsWith(name);
    System.out.println("productBrandList: " + productBrandList);
    return productBrandList;
}
Run Code Online (Sandbox Code Playgroud)

发出以下URL:

http://localhost:19191/productbrand/namestartswith?name=f
Run Code Online (Sandbox Code Playgroud)

生产:

{"productBrand":[{"brandImage":"ffbrand.png","description":"the brand called ff","id":"1","name":"ffbrand"},{"brandImage":"flfl.png","description":"flfl","id":"6","name":"flfl"},{"brandImage":"ffbran.png","description":"ffbr","id":"16","name":"ffbran"}]}
Run Code Online (Sandbox Code Playgroud)

这意味着该服务按预期工作.

现在我使用RestEasy进行客户端访问.

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

以下代码访问服务:

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:19191/productbrand/namestartswith?name=" + name);
    Response restEasyResponse = target.request(MediaType.APPLICATION_JSON).get();
    log("entity: " + restEasyResponse.readEntity(new GenericType<List<ProductBrand>>() {
    }););
Run Code Online (Sandbox Code Playgroud)

输出是:

entity:null

甚至叫restEasyResponse.getEntity()回归null.可能有什么问题?

Gon*_*heu 6

我有类似的问题,我使用以下方法解决它: restEasyResponse.readEntity(List.class)

它将返回List <Map <String,Object >>,其中每个项表示json数组的元素.