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.


@TransactionAttribute(TransactionAttributeType.REQUIRED)
  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


@Path("countries")
 @POST
 @RBAC(type = { CRUDEnum.CREATE }, capability = { "HelloWorldCap" })
 @Consumes({
   "application/vnd.net.juniper.space.hello-world.country+json;version=1;charset=UTF-8",
   "application/vnd.net.juniper.space.hello-world.country+xml;version=1;charset=UTF-8" })
 @Produces({
   "application/vnd.net.juniper.space.hello-world.country+json;version=1",
   "application/vnd.net.juniper.space.hello-world.country+xml;version=1" })
 public Country addCountry(Country param0);


REST Implementation for POST Method


public Country addCountry(Country param0) {
 /**
  * TODO: Put addCountrylogic using
  * net.juniper.jmp.helloworld.ejb.HelloWorld bean instance
  * 
 */

 net.juniper.jmp.helloworld.ejb.Country country = null;
  try {
	  country = new net.juniper.jmp.helloworld.ejb.Country();
  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 (net.juniper.jmp.helloworldsvc.v1.Country.States.State source : param0
		.getStates().getStates()) {
	  net.juniper.jmp.helloworld.ejb.State dest = new net.juniper.jmp.helloworld.ejb.State();
	  PropertyUtils.copyProperties(dest, source);
	  col1.add(dest);
	 }
	  country.setStates(col1);
     }
	  country = getBean()
	 .addCountry(new InternalPagingContext(), country);
		// reverse copy code

  net.juniper.jmp.helloworldsvc.v1.Country toReturn = new net.juniper.jmp.helloworldsvc.v1.Country();
	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 (net.juniper.jmp.helloworld.ejb.State source : country
		 .getStates()) {
	   net.juniper.jmp.helloworldsvc.v1.Country.States.State dest = new net.juniper.jmp.helloworldsvc.v1.Country.States.State();
	   PropertyUtils.copyProperties(dest, source);
	   colRev1.add(dest);
	  }
	  }
  net.juniper.jmp.helloworldsvc.v1.Country.States inner = new net.juniper.jmp.helloworldsvc.v1.Country.States();
	inner.setStates(colRev1);
	toReturn.setStates(inner);
	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) {
		 throw new WebApplicationException(Response.status(400).entity(
		 "Invalid request, unable to add country in the database.")
		 .build());
	  } 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/vnd.net.juniper.space.hello-world.countries+xml;version=1 POST: api/space/hello-world/countries Content-Type--- application/vnd.net.juniper.space.hello-world.country+xml;version=1 Test Country 6000

HTTP GET Response


Status Code: 200 OK
Content-type: application/vnd.net.juniper.space.hello-world.country+json;version=1 {"country":{"states":"","id":65537,"name":TestCountry","population":7777}}