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


This topic explains about the REST PUT method. In HelloWorld application, "updateCountry" method is RESTified to update a country in the database. Firstly, an EJB method is created, and then the same method is RESTified for updating 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 creation for EJB implementation, RESTification, and validation of the method using REST client.

Step 1: Create EJB method for "updateCountry"

This step creates an EJB method "updateCountry" to update a country in the database.

public Country updateCountry(ApiContextInterface ctx, int id,
    Country country) throws Exception {
  CountryEntity countryEntity = manager.find(CountryEntity.class, id);
  if (countryEntity == null)
    throw new javax.naming.NameNotFoundException(country.getName() + " with id " + id + " was not found.");
  if (country.getName() != null && !(country.getName().trim().equals("")))
  if (country.getPopulation() != null) {
    if (country.getPopulation() > 0)
      throw new IllegalArgumentException("Invalid population");

  EntityManagerWrapper qm = new EntityManagerWrapper(manager);
  Collection states = country.getStates();
  if (states != null && !states.isEmpty()) {
      for (State state : states) {
        int stateId = state.getId();
        logger.log(Level.INFO, "updating state with Id-" + stateId);
        updateState(ctx, stateId, state);


  qm.merge(countryEntity, ctx);
  return getCountry(countryEntity.getCountry());

Step 2: RESTify EJB method for getCountries

This step generates a corresponding REST method to update 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. 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. Click Add. The Configure REST Method ( updateCountry ) screen is displayed.

  7. The above screen contains the following fields:

  8. Enter the appropriate details.

  9. Click Next. The Configure REST Method ( updateCountry ) 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 ( updateCountry ) screen is displayed.

  14. Select the appropriate media type.

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

REST Interface for PUT Method

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

@RBAC(type = { CRUDEnum.UPDATE }, capability = { "HelloWorldCap" })
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+xml;version=1;charset=UTF-8",
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+json;version=1;charset=UTF-8" })
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+xml;version=1",
        VendorConstants.APP_DATATYPE_PREFIX + ".world.country+json;version=1" })
public Country updateCountry(Country country, @Context UriContext param1,
        @PathParam("countryId") int param2);

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 PUT Method

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

public Country updateCountry(Country param0, UriContext param1, int param2) {

  int a2 = param2;
  vnd.jssdk.helloworld.Country country = null;
  try {

    country = new vnd.jssdk.helloworld.Country();
    copyRestToEjbForCountry(country, param0);
    country = getBean().updateCountry(new InternalPagingContext(), a2,
    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(
        .entity("Unable to update, country with the given name already exists.")
  } catch (Exception e) {
    throw new WebApplicationException(
        .entity("Invalid request, unable to update country in the database.")
  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 PUT Request

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


HTTP PUT 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"?>

  <name>My Country Updated</name>