Developing a JEE REST API on IBM Bluemix

On our latest post we quickly show you how to create a simple web project Get Started with Bluemix

On this post we will create a JEE REST application that will be the base for our next post that we will include a database connection with CRUD(Create, Read, Update and Delete) basic functionalities.

For this project we will create a local Liberty WebSphere so we can test the app before we deploy it into Bluemix. This allow us to have all the required libraries at hand.

We suppose you have already sign-up to Bluemix and install the Bluemix Eclipse plug-in . On this step we will setup the project and add an empty REST service.

1. Create a new WebSphere liberty server

After you have install the Bluemix plugin you can do the following:
– Go to the Servers Tab
– Click right button on the empty space and select new server
– Select WebSphere Application Server
– Select Download and install a new runtime environment, from the liberty profile and choose Liberty Profile V8.5.5.2 Runtime
– Choose the Liberty Profile Extended Package
– Accept the Term and Conditions, then the target installation folder.

2. Create a new dynamic web project

– Go to file -> new dynamic web project
– Create a new project with the target runtime WebSphere Application Server
– Select next, until you get asked to configure web module settings and generate the web.xml and finish

3. Create a JAX-RS POJO class

On this step we will create the basic classes to test the rest services
– Create a new Class that will provide the rest services connection


package co.at3.peopleadmin.services;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import co.at3.peopleadmin.model.Person;

@Path("/people")
@Stateless
public class PeopleAdminService {

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Person> list() {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person(1, "Perez", "Pedro", "Colombia", "Bogotá"));
persons.add(new Person(1, "Perez", "Maria", "Colombia", "Medellín"));
return persons;
}

@GET
@Path("find/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Person find(@PathParam("id") Integer id) {
return new Person(1, "Perez", "Pedro", "Colombia", "Bogotá");
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void create(Person person) {
System.out.println("Create Person : " + person.toString());
}

@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void modify(Person person) {
System.out.println("Modify Person : " + person.toString());
}

@DELETE
@Path("{id}")
public void delete(@PathParam("id") Integer id) {
System.out.println("Delete Person : " + id);
}

}

– Now Create an Application Class that will publish the REST services

public class PeopleAdminApplication extends Application{
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add( co.at3.peopleadmin.services.PeopleAdminService.class);
return classes;
}
}

– Finally Create the entity class

package co.at3.peopleadmin.model;
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name="t_person")
public class Person implements Serializable {

private static final long serialVersionUID = -5256770408245841682L;
@Id
private Integer id;
....
}

4. Modify the web.xml

On this section we will publish the services using the web.xml

– Modify the web.xml file as follows

<!--?xml version="1.0" encoding="UTF-8"?-->

PeopleAdmin

default.html

co.at3.peopleadmin.services.PeopleAdminApplication

co.at3.peopleadmin.services.PeopleAdminApplication
/api/*

– Publish fist locally and test that everything is working so far, and then upload to Bluemix and test opening the url ending with “/api/people”, you should see the list of persons on Json format

NOTE:To publish on the local server you have to modify the server.xml and add a new featurejaxrs-1.1

Code

https://hub.jazz.net/project/advtek/PeopleAdmin/overview

References

https://developer.ibm.com/bluemix/2014/04/07/ejbs-bluemix-jpa-db2/