Resteasy JAX-RS客户机返回null的实体
我有一个定义如下的JAX-RS服务:Resteasy JAX-RS客户机返回null的实体,resteasy,Resteasy,我有一个定义如下的JAX-RS服务: @Produces(MediaType.APPLICATION_JSON) @GET @Path("/namestartswith") public List<ProductBrand> nameStartsWith(@QueryParam("name") String name) { List<ProductBrand> productBrandList = productBrandService.findByNameSta
@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;
}
产生:
{"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>
org.jboss.resteasy
resteasy客户端
${resteasy.version}
org.jboss.resteasy
resteasy jackson提供商
${resteasy.version}
以下代码访问该服务:
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>>() {
}););
Client-Client=ClientBuilder.newClient();
WebTarget=client.target(“http://localhost:19191/productbrand/namestartswith?name=“+姓名);
Response restEasyResponse=target.request(MediaType.APPLICATION_JSON.get();
日志(“实体:”+restEasyResponse.readEntity(新的GenericType)(){
}););
输出为:
实体:空
即使调用
restEasyResponse.getEntity()
也会返回null
。可能有什么问题?我遇到了类似的问题,我使用以下方法解决了这个问题:
restEasyResponse.readEntity(List.class)
它将返回一个列表,其中每个项表示json数组的一个元素
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>>() {
}););