2014-10-17 62 views
5

的实体我有这样定义的JAX-RS服务:JAX-RS客户端返回null

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

发出以下网址:

http://localhost:19191/productbrand/namestartswith?name=f 

生产:

{"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"}]} 

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

现在我使用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> 

下面的代码来访问服务:

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>>() { 
    });); 

的输出是:

实体:空

即使主叫restEasyResponse.getEntity()返回null。什么可能是错的?

回答

3

我也有类似的问题,我用解决它: restEasyResponse.readEntity(List.class)

它会返回一个列表<地图<字符串,对象> >,其中每个项目代表JSON数组的元素。

相关问题