In this workshop you will learn about the following:
Here are a list of prerequisites you will need in order to complete this workshop.
You should already have the Cloud Foundry Command Line Tools installed, if not go to the prerequisites slide and follow the link to the install instructions.
Deploy the application to Bluemix using the Cloud Foundry CLI by running the following command
After about 30 seconds your app should be deployed to Bluemix. Refresh the browser to see the new UI or go back to Bluemix and select the URL to launch it again. You should now see the ToDo UI instead of the sample application.Watch Demonstration
While our ToDo application successfully deployed to Bluemix you will notice that it is not functioning properly yet. Open your browsers developer console and reload the page, you will notice there is an error. This is because the web application is making a request to a URL that does not exist yet.Watch Demonstration
There are several REST APIs that need to be implemented in order to make our ToDo application work.
The JSON representation of a ToDo looks like this
We are going to want to persist our ToDos in a database so when a user comes back to the application or refreshes the page they will see the same ToDos. In our application we will choose to use Cloudant.
We are going to need a DB in Cloudant to actually use in our application, so lets create one.
Now we are ready to implement our GET request. Add the following method to ToDoRestController
The @GET annotation tells JAX-RS to call the getAll method of this class when a GET request is made to endpoint /api/todos. See how simple this code becomes when using our repository class and Jackson to serialize our POJO?
Add the following code to ToDoRestController to implement the POST method.
The @POST annotation tells JAX-RS to call the create method when a POST request is made to /api/todos. The td parameter in the method is the serialized JSON object from the POST body. We then just use our repository to save that object to our Cloudant DB.
Add the following code to ToDoRestController to implement the PUT method.
We first get the ToDo from the DB that is specified in the id path parameter, then we update it with the data from the ToDo JSON from the PUT body. Finally we insert the ToDo back into the DB and return the JSON back to the client.
Add the following code to ToDoRestController to implement the DELETE method.
We first get the ToDo from the DB with the ID that is specified in the path parameter. Then we use the repository to delete the ToDo from the DB.
The last thing we need to do is let our app know where the DB is located
We can use a manifest file to specify parameters for our deployment to Bluemix. Manifests make our cf push command simple and consistent by taking all the parameters we would normally specify on the command line and putting them in a file.
If you would like an easy way of generating these files check out the CF Manifest Generator.
The Liberty buildpack, which is used by default when running a Java application in Bluemix, contains a feature called auto-configuration. For certain service in Bluemix, like Cloudant, the auto-configuration feature will detect the Cloudant service is bound to the application and try and provide our application with jars it thinks we will need to leverage the service. In our case we don't need these jars because they are included with our application. You can opt out of auto-configuration by using an environment variable. For more information see the documentation.
Notice we don't need to specify any parameters to cf push. That is because it will automatically look for, and use the manifest.yml file if it finds one. In about 30 seconds your deployment should finish and your application should be deployed. Head to your application URL and try it out.Watch Demonstration