HTTP GET Method Demonstration

This section gives the demonstration of the GET Method used in Hello World.

Step 1: Create EJB method for getCountries.

This method is used to get all the countries present in the system.

Note: Refer to the Sorting, Paging, Filtering, and Field section for the usage and description of Apicontext parameter.


@SuppressWarnings("unchecked")
  @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 the countries from the EJB.

The below Fig shows getCountries(...) method is selected to RESTify. Click "Next" to configure the DTO. Note: If you want to use Filtering/Sorting, support must be provided at the EJB layer.




The following screen shows Rest Method details. The Method Signature preview can be seen here. Click "Next".



Select the fields for the DTO by checking "Property Name" and Click "Configure".



After configuring the property, click Finish. You will be asked to proceed through the rest of the wizard.



Click "Finish" complete the wizard.

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


@Path("countries")
 @GET
 @RBAC(type = { CRUDEnum.READ }, capability = { "HelloWorldCap" })
 @Produces({
  "application/vnd.net.juniper.space.hello-world.countries+json;version=1",
  "application/vnd.net.juniper.space.hello-world.countries+xml;version=1" })
  public net.juniper.jmp.helloworldsvc.v1.Countries getCountries(
  @Context UriContext param0);

REST Implementation for GET Method


public net.juniper.jmp.helloworldsvc.v1.Countries getCountries(
 UriContext param0) {
  /**
   * TODO: Put getCountrieslogic using
   * net.juniper.jmp.helloworld.ejb.HelloWorld bean instance
   * 
  */

  net.juniper.jmp.helloworldsvc.v1.Countries countries = null;
  net.juniper.jmp.PagingContext ctx = param0.getPagingCtxt();
	try {
	countries = new net.juniper.jmp.helloworldsvc.v1.Countries();
	java.util.Collection restCountries = new java.util.ArrayList();
	net.juniper.jmp.PagingResult ejbCountryList = getBean()
	  .getCountries(ctx);
	if (ejbCountryList == null) {
	   throw new WebApplicationException(Response.Status.NOT_FOUND);
	}
	for (net.juniper.jmp.helloworld.ejb.Country country : ejbCountryList) {
	   net.juniper.jmp.helloworldsvc.v1.Countries.Country restCountry = new net.juniper.jmp.helloworldsvc.v1.Countries.Country();
	   PropertyUtils.copyProperties(restCountry, country);

	   restCountries.add(restCountry);
	}
	countries.setCountries(restCountries);
	countries.setTotalSize(ejbCountryList.getPagingContext()
	  .getTotalRecords());
	} catch (Exception e) {
	  throw new WebApplicationException(e);
	} finally {
	  // TODO: Auto generated code
	}

	return countries;
	}
	

Step 3: Invoke REST API using REST Client.

You need to use REST Client to invoke REST API created in Step 2. Assuming that the application is already 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/space/hello-world/countries
HTTP/1.1 Host: <host-name>:<port>
Accept: application/vnd.net.juniper.space.hello-world.countries+xml;version=1

HTTP GET Response


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<countries uri="/hello-world/countries/" size="3">

<country uri="/hello-world/countries/3"> 
<id>3</id> 
<name>Indonesia</name> 
<population>231373</population>  
</country> 

<country uri="/hello-world/countries/4"> 
<id>4</id> 
<name>Brazil</name> 
<population>192994</population>  
</country> 

<country uri="/hello-world/countries/5"> 
<id>5</id> 
<name>Pakistan</name> 
<population>169605</population>  
</country> 

............