This topic explains about the REST POST method. In the HelloWorld application, the "addcountry" method is RESTified to add a country in the database. Firstly an EJB method is created, then the same method is RESTified for adding 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 EJB implementation, RESTification, and validation of the method using REST client.
This step creates an EJB method "addCountry" to add a country in the database.
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Country addCountry(ApiContextInterface ctx, Country country) throws Exception {
if ((country != null) && (country.getPopulation() == null || country.getPopulation() < 0 || country.getName()
.trim().equals("")))
throw new IllegalArgumentException("Invalid population or invalid country name");
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());
}
This step generates a corresponding REST method to add country from the EJB. Find below the step by step procedure to RESTify EJB method through HelloWorld Wizard.
The above screen contains the following fields:
The RESTified interface code generated by the REST creation wizard is shown below:
@Path("countries")
@POST
@RBAC(type = { CRUDEnum.CREATE }, capability = { "HelloWorldCap" })
@Consumes({
VendorConstants.APP_DATATYPE_PREFIX + ".world.country+xml;version=1;charset=UTF-8",
VendorConstants.APP_DATATYPE_PREFIX + ".world.country+json;version=1;charset=UTF-8" })
@Produces({
VendorConstants.APP_DATATYPE_PREFIX + ".world.country+xml;version=1;",
VendorConstants.APP_DATATYPE_PREFIX + ".world.country+json;version=1;" })
public Country addCountry(Country country);
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".
* Method: addCountry
* 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
* @return: vnd.jssdk.helloworld.rest.v1.Country
public Country addCountry(Country param0) {
vnd.jssdk.helloworld.Country country = null;
try
{
country = new vnd.jssdk.helloworld.Country();
copyRestToEjbForCountry(country, param0);
country = getBean().addCountry(new InternalPagingContext(), country);
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(
Response.status(400)
.entity("Unable to add, country already exists in the database.")
.build());
} catch (Exception e) {
e.printStackTrace();
throw new WebApplicationException(
Response.status(400)
.entity("Invalid request, unable to add country in the database.")
.build());
}
return param0;
}
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.POST : /api/jssdk/hello-world/world/countries
HTTP/1.1 Host : <host-name>:<port>
Authorization : Basic c3VwZXI6anVuaXBlcjEyMw==
Accept : application/vnd.jssdk.hello-world.world.country+xml;version=1
Content-Type : application/vnd.jssdk.hello-world.world.country+xml;version=1
<country>
<name>Test Country</name>
<population>6000</population>
</country>
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" ?>
<country>
<state/>
<id>18</id>
<name>Test Country</name>
<population>6000</population> ;
</country>