Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Reference Applications > HelloWorld > HelloWorld Code Walk-Through

HTTP DELETE Method - HelloWorld

This topic describes the REST DELETE method. In the HelloWorld application, the "deleteCountry" method is RESTified to delete a country in the database. Firstly, an EJB method is created, then the same method is RESTified for deleting a countries. The method is validated by invoking REST API and generating result through REST client. Find below the step-by-step procedure to understand EJB implementation, RESTification, and validation of the method using REST client.

Step 1: Create EJB method for "deleteCountry".

This step creates an EJB method "deleteCountry" to delete a country in the database.

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void deleteCountry(ApiContextInterface ctx, int id) throws Exception {
  if (ctx == null) {
    ctx = new InternalPagingContext();
  }
  CountryEntity countryEntity = manager.find(CountryEntity.class, id);
  if (countryEntity == null)
    throw new javax.naming.NameNotFoundException("Country with id " + id + " was not found.");

  EntityManagerWrapper qm = new EntityManagerWrapper(manager);
  Collection col = countryEntity.getState();
  if (col != null && !col.isEmpty()) {
    for (StateEntity se : col) {
      int stateId = se.getId();
      logger.log(Level.INFO, "Deletion of State with id:" + stateId);
      deleteState_api(ctx, stateId);

    }
  }
  qm.remove(countryEntity, ctx);

}

Step 2: RESTify EJB method for "deleteCountry"

This step generates a corresponding REST method to delete a country from the EJB. Find below the step by step procedure to RESTify EJB method through HelloWorld Wizard.

  1. Go to HelloWorldEJB > src > vnd.jssdk.helloworld.HelloWorld.java. Right click on the application interface file. The below screen is displayed.



  2.  
  3. Click on the Create/Edit REST Resources sub-menu item. The REST Interface Creation screen is displayed.

  4. Enter the appropriate details.



  5.  
  6. Click Next. The Configure REST Resource screen is displayed.



  7.  
  8. Select the appropriate method from Methods of EJB Interface.

  9. Click Add. The Configure REST Method ( deleteCountry ) screen is displayed.



  10.   The above screen contains the following fields:


  11. Click Next. The Configure REST Method ( deleteCountry ) screen is displayed.



  12.  
  13. Select the appropriate RBAC details.

  14. Click Next. The Configure REST Method ( deleteCountry ) screen is displayed.




  15.  
  16. Select the appropriate media type.

  17. Click Finish. The Configure REST Resource screen is displayed.



  18.  

REST Interface for DELETE Method

The RESTified interface code generated by the REST creation wizard is shown below:

@Path("countries/{id}")
@DELETE
@RBAC(type = { CRUDEnum.DELETE }, capability = { "HelloWorldCap" })
  public void deleteCountry(@PathParam("id") int param0);
  

REST Implementation for DELETE Method

/**
 * Method: deleteCountry
 * Description: This is an  auto generated method with stub implementation which uses
  the vnd.jssdk.helloworld.HelloWorld EJB bean and exposes it's method with Rest web services interface.
 * @param: UriContext
 * @param: int
 * @return: void
 */

public void deleteCountry(UriContext param0, int param1) {

  try {

    int a1 = param1;
    getBean().deleteCountry(null, a1);
  } catch (WebApplicationException e) {
    throw e;
  } catch (EntityNotFoundException e) {
    throw new WebApplicationException(
        Response.status(404)
        .entity("Unable to delete, country with the given id do not exist.")
        .build());
  } catch (Exception e) {
    throw new WebApplicationException(
        Response.status(400)
        .entity("Invalid request, unable to delete country in the database.")
        .build());
  }
  return;
}

Step 3: Invoke REST API using REST Client.

Use REST Client to invoke REST API created in Step 2. Assumption is that the application is built and deployed on the Junos Space. For building and deploying application, please refer to the Building and Deploying HelloWorld section.

HTTP DELETE Request

DELETE        : api/jssdk/hello-world/world/countries/{countryId}
HTTP/1.1 Host : <host-name>:<port>
Authorization : Basic c3VwZXI6anVuaXBlcjEyMw==

HTTP DELETE Response

Status Code : 204 No Content