Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Developing Junos Space Applications > Creating REST Services

Consuming REST Services Using the Jersey Client

Jersey is an Open Source Reference implementation for creating REST Web services. It provides client API(s) and libraries to consume REST Web services.

This topic demonstrates how to use JSServiceClient to get a reference of Jersey Client object. It further demonstrates how to use this reference to perform CRUD operations. This section provides:
  1. Sample Code snippet using Jersey Client to consume REST Web services.
  2. Utility Methods on how to use Jersey.
  3. References on how to use Jersey.

Sample Code snippet using Jersey Client

HTTP POST


/** This API demonstrates Jersey Client API (HTTP POST usage) to add a User 
 *  It takes ApiContext as a parameter.
 **/

public String addUser(ApiContext apic){
  /** Get a Jersey Client Object using JSServiceClient **/
  Client client = setupHttpClient(apic); // 1

  /* It is important here to type case ApiContext to InternalApiContext */
  InternalApiContext iac = (InternalApiContext)apic; //2
  try {

    /** Get Base Url **/
    String baseUrl = iac.getBaseUrl(); //3
    /** Setup the http URL **/
    String url = baseUrl+ "/api/space/user-management/users"; 
    String mediaType = "application/vnd.net.juniper.space.user-management.user+xml;version=1;charset=UTF-8";
    /** Get the Client Response Object **/
    final String requestBody = "testfname";
    ClientResponse response = postRequest(client, url, mediaType,requestBody);

    /** Error handling to check if the status code is 200 or no **/ // 4
    if (response.getClientResponseStatus() == ClientResponse.Status.OK)
    {
      /** Get a String representation of the response body using response.getEntity() **/
      String user = response.getEntity(String.class);
      return user;
    }
    else
    {
      return null;
    }

  } catch (Exception e) {            
    // Log exception here.
  }
  return null;  
}


HTTP GET


/** This API demonstrates Jersey Client API (HTTP GET usage) to fetch user details  **/

public String getUserByID(ApiContext apic) {
// Setup HttpClient and and InternalApiContext by following Steps 1 and 2 from addUser API.
   try 
   {
      // Setup BaseUrl by following Step 3 from addUser API.		
      String url = baseUrl+ "/api/space/user-management/users/65536";	

      String mediaType = "application/vnd.net.juniper.space.user-management.user+xml;version=1"; 
      /** Get the Client Response Object **/

      ClientResponse response = getResponse(client, url , mediaType);		
      / ** Perform Error Handling by following Step 4 from addUser API **/
	
   } catch (Exception e) {            
     // Log exception here.
   }
   return null;
}


HTTP PUT


/** This API demonstrates Jersey Client API (HTTP PUT usage) to update an existing User **/

public String editUser(ApiContext apic) {
// Setup HttpClient and and InternalApiContext by following Steps 1 and 2 from addUser API.
  try 
  {         
     // Setup BaseUrl by following Step 3 from addUser API.  
     String url = baseUrl+ "/api/space/user-management/users/65536"; 
     String mediaType = "application/vnd.net.juniper.space.user-management.user+xml;version=1;charset=UTF-8"; 

     final String requestBody = "testnewFname";
    /** Get the Client Response Object **/

    ClientResponse response = putRequest(client, url , mediaType, requestBody);
    /** Perform Error Handling by following Step 4 from addUser API **/
  } catch (Exception e) {            
    // Log exception here.
  }
  return null;     
}


HTTP DELETE


/** This API demonstrates Jersey Client API (HTTP PUT usage) to delete an existing User **/

public void deleteUser(ApiContext apic) {
   
   // Setup HttpClient and and InternalApiContext by following Steps 1 and 2 from addUser API.
   try {            

        // Setup BaseUrl by following Step 3 from addUser API.				
        String url = baseUrl+ "/api/space/user-management/users/65536";

        String mediaType = "application/vnd.net.juniper.space.user-management.user+xml;version=1;charset=UTF-8"; 		
        /** Get the Client Response Object **/

       ClientResponse response = deleteRequest(client, url , mediaType);
       /** Perform Error Handling by following Step 4 from addUser API **/
	
      } catch (Exception e) {            
	   // Log exception here.
      }
   return null;   
}


Utility Methods

  1. Setting up HTTP Client
  2. Read object using HTTP GET
  3. Create object using HTTP POST
  4. Update object using HTTP PUT
  5. Delete object using HTTP DELETE

Setting up HTTP Client


/* This is a utility method to get the Jersey Client object using JSServiceHttpClient
 * This method takes ApiContext as an argument 
 */
 
public Client setupHttpClient(ApiContext apic) {
// Create an instance of JSServiceClient using ApiContext
   try {

     JSServiceClient client = new JSServiceClient(apic);
     Client jerseyClient = client.jerseyClient();
	 return jerseyClient;
  } catch (Exception exc) {

    logger.error(ERROR_INITCLIENT);
	logger.error(exc);
  }
 return null;
}


Read object using HTTP READ


/* This is a utility method to get the ClientResponse from a url */

private ClientResponse getResponse(Client jerseyClient, String url, String mediaType) {
  ClientResponse response = null;
   try {

 /** HTTP GET request **/
 response = jerseyClient.resource(url).accept(mediaType)
			.get(ClientResponse.class);

  } catch (Exception e) {
     logger.error(NO_RESPONSE);
     logger.error(e);
  }
 return response;
}

Create object using HTTP POST


/* This is a utility method to post the message **/

private ClientResponse postRequest(Client jerseyClient, String url, String mediaType, final String requestBody){
	
 ClientResponse response = null;
  try {

  /** HTTP POST request request **/
  response = jerseyClient.resource(url).accept(mediaType).
  post(ClientResponse.class, requestBody);

  } catch (Exception e) {
     logger.error(NO_RESPONSE);
     logger.error(e);
  }
 return response;			
}

Update object using HTTP PUT


/* This is a utility method to update and existing object using HTTP PUT **/

private ClientResponse putRequest(Client jerseyClient, String url, String mediaType, final String requestBody)
{	
  ClientResponse response = null;
   try {

  /** HTTP PUT request request **/
  response = jerseyClient.resource(url).accept(mediaType).
  put(ClientResponse.class, requestBody);

  } catch (Exception e) {
     logger.error(NO_RESPONSE);
     logger.error(e);
  }
 return response;			
}

Delete object using HTTP DELETE


/* This is a utility method to delete an object using HTTP DELETE **/

private void deleteRequest(Client jerseyClient, String url, String mediaType)
{	
 try {

     /** HTTP DELETE request request **/
     jerseyClient.resource(url).accept(mediaType).delete();

  } catch (Exception e) {
     logger.error(NO_RESPONSE);
     logger.error(e);
  }
				
}

References on how to use the Jersey Client

To develop your application using Jersey Client libraries, refer to https://jersey.java.net/documentation/latest/client.html