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

HTTP GET Method - HelloWorld

This topic explains about the REST GET method. In the HelloWorld application, the "getCountries" method is RESTified to get all the countries in the database. Firstly an EJB method is created, then the same method is RESTified for getting countries. 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 "getCountries"

Below steps create an EJB method "getCountries" to fetch countries from the database.

@Cursor(entityClass = CountryEntity.CLASSNAME, queryName = CountryEntity.GET_ALL_COUNTRIES)
public PagingResult getCountries(PagingContext ctx) throws Exception {
  EntityManagerWrapper qm = new EntityManagerWrapper(manager);
  List countryEntities = qm.executeNamedQuery(CountryEntity.GET_ALL_COUNTRIES, ctx);
  if (countryEntities == null) {
    return null;
  }
  PagingResult countries = new PagingResult();
  for (CountryEntity countryEntity : countryEntities) {
    countries.add(fromCountryEntity2Country(countryEntity));
  }
  return countries;
}

Step 2: RESTify EJB method for "getCountries".

This step generates a corresponding REST method to get all countries from the EJB. Find below the procedure to RESTify EJB method through HelloWorld Wizard. The below figure shows "getCountries" method is selected to RESTify.

  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. The above screen contains the following fields:


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




  6. Select the appropriate method from Methods of EJB Interface.

  7. Click Add. The Configure REST Method ( getCountries ) screen is displayed.





  8. The above screen contains the following fields:


  9. Enter the appropriate details.

  10. Click Next. The Configure REST Method ( getCountries ) screen is displayed.


  11. Select the appropriate RBAC details.

  12. Click Next. The Configure REST DTO ( Countries ) screen is displayed.


  13. Select the appropriate fields for the DTO.

  14. Click Configure. The Configure A Property ( Primary Collection ) screen is displayed.


  15. Configure the appropriate property.

  16. Click Finish. The Configure REST Method ( getCountries ) screen is displayed.


  17. Select the appropriate media type.


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




  19. Note: Click Finish on the main wizard if you do not wish to RESTify other methods. If you want to RESTify other methods, use the preceding steps.

REST Interface for GET Method

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

@Path("countries")
@GET
@RBAC(type = { CRUDEnum.READ }, capability = { "HelloWorldCap" })	
@Produces({
	VendorConstants.APP_DATATYPE_PREFIX + ".world.countries+xml;version=1",
	VendorConstants.APP_DATATYPE_PREFIX + ".world.countries+json;version=1" })
public vnd.jssdk.helloworld.rest.v1.Countries getCountries(
	@Context UriContext param0);
	

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

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

public vnd.jssdk.helloworld.rest.v1.Countries getCountries(UriContext param0) {

  vnd.jssdk.helloworld.rest.v1.Countries countries = null;
  net.juniper.jmp.PagingContext pagingCtx = param0.getPagingCtxt();

  try {
    countries = new vnd.jssdk.helloworld.rest.v1.Countries();
    java.util.ArrayList restCountries = new java.util.ArrayList();
    net.juniper.jmp.PagingResult ejbCountryList = getBean().getCountries(pagingCtx);
    if (ejbCountryList == null || ejbCountryList.size() == 0) {
      return null;
    }
    for (vnd.jssdk.helloworld.Country country : ejbCountryList) {
      vnd.jssdk.helloworld.rest.v1.Countries.Country restCountry = new vnd.jssdk.helloworld.rest.v1.Countries.Country();
      copyComplexPropEjbToRestForCountries(restCountry, country);
      restCountries.add(restCountry);
    }
    countries.setCountries(restCountries);
    countries.setTotalSize(ejbCountryList.getPagingContext().getTotalRecords());
  } catch (WebApplicationException e) {
    throw e;
  } catch (Exception e) {
    throw new WebApplicationException(e);
  }
  return countries;
}

Step 3: Invoke REST API using REST Client.

You need to 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 GET Request

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

HTTP GET Response

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<countries uri="/api/space/hello-world/countries/" size="3">
  <country href="/api/jssdk/hello-world/world/countries/1" uri="/api/jssdk/hello-world/world/countries/1">
    <id>1</id>
    <name>Bangladesh</name>
    <population>1626021</population>
  </country>
  <country href="/api/jssdk/hello-world/world/countries/2" uri="/api/jssdk/hello-world/world/countries/2">
    <id>2</id>
    <name>Brazil</name>
    <population>19432994</population>
  </country>
  <country href="/api/jssdk/hello-world/world/countries/3" uri="/api/jssdk/hello-world/world/countries/3">
    <id>3</id>
    <name>China</name>
    <population>10518676</population>
  </country>