I have a JAX-RS service defined like this:
@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;
}
Issuing the following URL:
http://localhost:19191/productbrand/namestartswith?name=f
produces:
{"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"}]}
which means the service is working as intended.
Now I use RestEasy for client access.
<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>
The following code accesses the service:
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>>() {
}););
The output is:
entity: null
Even calling restEasyResponse.getEntity()
returns null
. What might be wrong?
Sample REST Client Let's begin writing a simple REST client. The getJsonEmployee () method retrieves an Employee object based on the employee id. The JSON returned by the REST Web Service is deserialized to the Employee object before returning. Using the JAX-RS API fluently to create web target, invocation builder and invoking a GET HTTP request:
As we saw in previous tutorials, JAX-RS supports extracting request values and mapping them into Java fields, properties and parameters using annotations such as @HeaderParam, @QueryParam, etc. JAX-RS also supports mapping of request body. In that case we don't have to use any JAX-RS annotations. such parameter is called entity parameter.
The value of the @DefaultValue is used when the corresponding meta-data is not present in the request. In the following examples, we will quickly see how to use @Default annotation. In each example, we are using JAX-RS Client API to make request to the target resource.
Additionally, we can also return Java based entities from a resource method, which can be automatically converted to JSON or XML depending on the requested 'Accept' header value. Please visits our example links provided in 'see also' section below.
I had a similar issue and I work around it using:
restEasyResponse.readEntity(List.class)
It will return a List<Map<String, Object>> where each item represents an element of the json array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With