Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS client returns entity of null

Tags:

resteasy

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?

like image 418
Sandah Aung Avatar asked Oct 17 '14 16:10

Sandah Aung


People also ask

How to write a REST client using JAX-RS API?

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:

What is entity parameter in JAX-RS?

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.

What is the use of @default annotation in JAX-RS?

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.

Can We return a Java entity from a resource method?

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.


1 Answers

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.

like image 145
Gonzalo Matheu Avatar answered Sep 19 '22 04:09

Gonzalo Matheu