REST JAVA Client and Error Handling

This section describes error codes and return values for different scenarios in HelloSpace application. You can view examples to understand the application and debug your own applications using Junos Space SDK. The following HTTP methods provide detailed information:

GET Request

This example shows the successful execution for an operation. It shows response as "OK" when you request a list of countries using GET method.


String request_URL = "http:///api/space/hello-again-space/countries";
// Set up credentials details.
      
Credentials credentials = new UsernamePasswordCredentials(USER, PASSWORD);

// Set up HTTPclient
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
ApacheHttpClientExecutor  clientExecutor = new ApacheHttpClientExecutor(httpClient);
ClientRequest request = new ClientRequest(request_URL, clientExecutor);
request.accept(mediaType);

// Execute HTTP GET request
ClientResponse response = request.get(String.class);
int status = response.getStatus();

// OK status  
if (status == Response.Status.OK.getStatusCode()) // OK!
{
    logger.info("Get All Countries");
}
else if (status == Response.Status.BAD_REQUEST.getStatusCode()
		|| status == Response.Status.NOT_FOUND.getStatusCode()) {
     logger.info("Unable to get Countries");

}

POST Request

This example shows the successful execution for an operation. It shows response as "OK" when you create a new country using POST method.


String request_URL = "http:///api/space/hello-space /countries";
try{
	Country country = new Country ();

// Set the " Country " object
 country.setName("Hassen");
 country.setPopulation(75L);
 ClientRequest request = new ClientRequest(request_URL, clientExecutor);
 request.header("Content-Type", contentType );
 request.body(contentType, country);
 ClientResponse response = request.post(String.class);

 int status = response.getStatus();

// "CREATED" status  
 if (status == Response.Status.OK.getStatusCode() || status == Response.Status.CREATED.getStatusCode()) // created!
 {
 logger.info("Added a New Country ";

// "BAD REQUEST" status.
 } else if(status ==  Response.Status.BAD_REQUEST.getStatusCode())
   {
   logger.info ("Adding already existing Country - " + response.getEntity());
   }
	} catch (Exception e) {
}

PUT Request

This example shows the successful execution for an operation. It shows response as "OK" when you update a country using PUT method.


String request_URL = "http:///api/space/hello-space/countries"+countryid;
try{

Country countryObject = new Country();
countryObject.setName("Hindustan");
countryObject.setPopulation(111111);
ClientRequest request = new ClientRequest(request_URL, clientExecutor);
request.header("Content-Type", contentType );
request.body(contentType,countryObject );
ClientResponse response = request.put(String.class);

int status = response.getStatus();

// "OK" status  
if (status == Response.Status.OK.getStatusCode()) // created!
 {
	logger.info("Updated Country with id: "+countryId);

// "BAD REQUEST" or "Not Found" status.
 } else if(status == Response.Status.BAD_REQUEST.getStatusCode()||status == Response.Status.NOT_FOUND.getStatusCode() ) //
 {
	logger.info("Unable to update country");
 }
 } catch (Exception e) {

}

DELETE Request

This example shows the successful execution for an operation. It shows response as "OK" when you delete a country using DELETE method.


try{
 String request_URL = "http:///api/space/hello-space/countries"+countryid;
 ClientRequest request = new ClientRequest(request_URL, clientExecutor);
 ClientResponse response = request.delete(String.class);
 int status = response.getStatus();
 if (status == Response.Status.NO_CONTENT.getStatusCode()) 
 {
   logger.info ("Country deleted : " + countryID );
 } else if(status == Response.Status.BAD_REQUEST.getStatusCode() ) 
 {
   logger.info("Deleting non existent State - " + response.getEntity());
				}
 } catch (Exception e) {
         e.printStackTrace();
}


Junos Space SDK provides additional features to validate test cases and codes such as: