HTTP POST Method Demonstration

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

Step 1: Create EJB method for addCountry.

This method is used to add a country in the database.

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

  public Country addCountry(ApiContextInterface ctx, Country country)
    throws Exception {

  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 getCountries.

This step generates a corresponding REST method to add the country from the EJB.

Select "addCountry" method to RESTify and click "Add"

The Rest method preview will be shown. Click "Next"

Configure the REST Method for RBAC. Click "next"

Select the fields for the DTO. Click "configure"

Click "Select All" to select all properties or check individual "properties" to configure the DTO. User can also define the "key" for the DTO and ca be selected from the drop down menu.
After selecting HATEOS property, Click "Finish"

Click "Finish" to complete the wizard.

Click "Finish" if no other methods needs to be RESTified.

REST Interface for POST Method

 @RBAC(type = { CRUDEnum.CREATE }, capability = { "HelloWorldCap" })
   "application/;version=1;charset=UTF-8" })
   "application/;version=1" })
 public Country addCountry(Country param0);

REST Implementation for POST Method

public Country addCountry(Country param0) {
  * TODO: Put addCountrylogic using
  * bean instance
 */ country = null;
  try {
	  country = new;
  try {
	PropertyUtils.copyProperties(country, param0);
	  } catch (Exception e) {
		// add handling here
  java.util.Collection col1 = new java.util.ArrayList();
  if (param0.getStates() != null
	  && param0.getStates().getStates() != null) {
	  for ( source : param0
		.getStates().getStates()) { dest = new;
	  PropertyUtils.copyProperties(dest, source);
	  country = getBean()
	 .addCountry(new InternalPagingContext(), country);
		// reverse copy code toReturn = new;
	try {
	    PropertyUtils.copyProperties(toReturn, country);
		} catch (Exception e) {
		 // add exception handling
	java.util.Collection colRev1 = new java.util.ArrayList();
	if (country != null && country.getStates() != null) {
	   for ( source : country
		 .getStates()) { dest = new;
	   PropertyUtils.copyProperties(dest, source);
	  } inner = new;
	param0 = toReturn;
	  } catch (EntityExistsException e) {
		throw new WebApplicationException(Response.status(400).entity(
		"Unable to add, country already exists in the database.")
	  } catch (Exception e) {
		 throw new WebApplicationException(Response.status(400).entity(
		 "Invalid request, unable to add country in the database.")
	  } finally {
		// TODO: Auto generated code

	return param0;


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 appliction 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/;version=1 POST: api/space/hello-world/countries Content-Type--- application/;version=1 Test Country 6000

HTTP GET Response

Status Code: 200 OK
Content-type: application/;version=1 {"country":{"states":"","id":65537,"name":TestCountry","population":7777}}