Sending query parameters via the JAX-RS client API

The JAX-RS client API provides an easy and straightforward way of sending query parameters to RESTful web services. The following example illustrates how to do this:

package com.ensode.javaee8book.queryparamsclient; 
 
import com.ensode.javaee8book.queryparamsclient.entity.Customer; 
import javax.ws.rs.client.Client; 
import javax.ws.rs.client.ClientBuilder; 
 
public class App { 
 
    public static void main(String[] args) { 
        App app = new App(); 
        app.getCustomer(); 
    } 
 
    public void getCustomer() { 
        Client client = ClientBuilder.newClient(); 
        Customer customer = client.target( 
                "http://localhost:8080/queryparams/resources/customer"). 
                queryParam("id", 1L). 
                request().get(Customer.class); 
 
        System.out.println("Received the following customer  
information:"); System.out.println("Id: " + customer.getId()); System.out.println("First Name: " +
customer.getFirstName()); System.out.println("Middle Name: " +
customer.getMiddleName()); System.out.println("Last Name: " + customer.getLastName()); } }

As we can see, all we need to do to pass a parameter is to invoke the queryParam() method on the instance of javax.ws.rs.client.WebTarget returned by the target() method invocation on our Client instance. The first argument to this method is the parameter name and must match the value of the @QueryParam annotation on the web service. The second parameter is the value that we need to pass to the web service. If our web service accepts multiple parameters, we can chain queryParam() method invocations, using one for each parameter our RESTful web service expects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.8