Loqate Local API On-Premise REST Web Service (LqtRestTestClient.java)

Sample Java Code: LqtRestTestClient.java


package com.loqate.ws.rest.client;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.net.URI;
import javax.ws.rs.core.MultivaluedMap;

/**
* Sample Test Client for Loqate Local API REST Web Service
* Queries the Loqate Local API REST Web Service with an address record.
* Displays response in XML and JSON format.
*
*/
public class LqtRestTestClient {
  public static void main(String[] args) {

  // Set input test values for web service query
  // Use of MultivaluedMap allows program to make URI web service call
  // passing parameters in a <parameter1> = <value_parameter1> format.
  MultivaluedMap queryParams = new MultivaluedMapImpl();
  queryParams.add("organization", "Loqate Inc.");
  queryParams.add("address", "1111 Bayhill Drive");
  queryParams.add("locality", "San Bruno");
  queryParams.add("country", "USA");
  queryParams.add("administrativeArea", "CA");
  queryParams.add("postalCode", "94066");

  // Client and WebResource configuration
  ClientConfig config = new DefaultClientConfig();
  Client client = Client.create(config);
  WebResource service = client.resource(getBaseURI());

  // Output shown below using multiple interfaces (XML and JSON)

  // XML response output
  System.out.print("XML Format Response: \n\n");
 System.out.println(
  service.queryParams(queryParams).accept(MediaType.TEXT_XML).get(String.class)
 );
  System.out.print("\n\n ********************* \n\n");

  // JSON response output
  System.out.print("JSON Format Response: \n\n");
 System.out.println(service.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(String.class));
   System.out.print("\n\n ********************* \n\n");
  }

  /**
  * Utility class used to build URI
  * @return URI for Loqate Local API REST Web Service
  *
  * Notice the URI includes the local server and selected port number
  * (localhost:8084), the application name (LqtWSRestApplication) and the
  * web service path defined by the REST annotation @Path in the web service
  * class (LqtLocalAPIService).
  */
  private static URI getBaseURI() {
   return UriBuilder.fromUri("http://localhost:8084/LqtWSRestApplication/resources/query/run").build();
  }

}