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

HTTP POST Method - HelloWorld

This topic explains about the REST POST method. In the HelloWorld application, the "addcountry" method is RESTified to add a country in the database. Firstly an EJB method is created, then the same method is RESTified for adding a country. The method is validated by invoking REST API and generating result through REST client. Find below the step-by-step procedure to understand the EJB implementation, RESTification, and validation of the method using REST client.

Step 1: Create EJB method for "addCountry"

This step creates an EJB method "addCountry" to add a country in the database.


@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Country addCountry(ApiContextInterface ctx, Country country) throws Exception {
  if ((country != null) && (country.getPopulation() == null || country.getPopulation() < 0 || country.getName()
  .trim().equals("")))
    throw new IllegalArgumentException("Invalid population or invalid country name");
  CountryEntity countryEntity = fromCountry2CountryEntity(country);
  EntityManagerWrapper qm = new EntityManagerWrapper(manager);
  qm.persist(countryEntity, ctx);
  Collection states = country.getStates();
  if (states != null && !states.isEmpty()) {
    for (State s : states) {
      logger.log(Level.INFO, "Adding state with Id-" + s.getId());
      addStateToCountry(ctx, s, countryEntity.getId());
    }
  }
  return getCountry(countryEntity.getCountry());
}

Step 2: RESTify EJB method for addCountry

This step generates a corresponding REST method to add 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. Click on the Create/Edit REST Resources sub-menu item. The REST Interface Creation screen is displayed.

  3. Enter the appropriate details.




  4. Click Next. The Configure REST Resource screen is displayed.

  5. Select the appropriate method from Methods of EJB Interface
  6. .
  7. Click Add. The Configure REST Method ( addCountry ) screen is displayed.



  8. The above screen contains the following fields:


  9. Click Next. The Configure REST Method ( addCountry ) screen is displayed.



  10. Select the appropriate RBAC details.

  11. Click Next. The Configure REST DTO ( Country ) screen is displayed.




  12. Select the appropriate fields for the DTO.

  13. Click Next. The Configure REST Method ( addCountry ) screen is displayed.




  14. Select the appropriate media type.

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


REST Interface for POST Method

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

@Path("countries")
@POST
@RBAC(type = { CRUDEnum.CREATE }, capability = { "HelloWorldCap" })
@Consumes({
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+xml;version=1;charset=UTF-8",
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+json;version=1;charset=UTF-8" })
@Produces({
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+xml;version=1;",
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+json;version=1;" })
public Country addCountry(Country country);

Note: The media types are generated as per vendor ID provided at the time of installation. For the reference application it is retrieved from the VendorConstants class at the EJB layer and is pre-determined as "jssdk".

REST Implementation for POST Method

* Method: addCountry
* 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: Country
* @return: vnd.jssdk.helloworld.rest.v1.Country

public Country addCountry(Country param0) {

  vnd.jssdk.helloworld.Country country = null;
  try 
  {
    country = new vnd.jssdk.helloworld.Country();
    copyRestToEjbForCountry(country, param0);
    country = getBean().addCountry(new InternalPagingContext(), country);
    vnd.jssdk.helloworld.rest.v1.Country toReturn = new vnd.jssdk.helloworld.rest.v1.Country();
    copyEjbToRestForCountry(toReturn, country);
    param0 = toReturn;
  } catch (EntityExistsException e) {
     throw new WebApplicationException(
        Response.status(400)
        .entity("Unable to add, country already exists in the database.")
        .build());
  } catch (Exception e) {
    e.printStackTrace();
    throw new WebApplicationException(
        Response.status(400)
        .entity("Invalid request, unable to add country in the database.")
        .build());
  }

  return param0;
}

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 POST Request

POST          : /api/jssdk/hello-world/world/countries
HTTP/1.1 Host : <host-name&gt:<port>
Authorization : Basic c3VwZXI6anVuaXBlcjEyMw==
Accept        : application/vnd.jssdk.hello-world.world.country+xml;version=1
Content-Type  : application/vnd.jssdk.hello-world.world.country+xml;version=1

<country>
  <name>Test Country</name>
  <population>6000</population>
</country>

HTTP POST Response

Status Code  : 200 OK
Content-type : application/vnd.jssdk.hello-world.world.country+xml;version="1"

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<country>
  <state/>
  <id>18</id>
  <name>Test Country</name>
  <population>6000</population> ;
</country>