Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 3: Spark

Matthew Tyson | Jan. 8, 2016
An extra lightweight, flexible, and scalable architecture for single-page web apps.

In Listing 7 we obtained a JDBC dataSource instance, which we'll use when connecting to the database instance running on localhost. In a true production scenario we'd need to do something about connection pooling, but we'll side-step that for the present. (Note that you'll want to change the root and password placeholders above to something unique for your own implementation.)

Updating the controller

Now let's return to the controller and update it. The updated controller shown in Listing 8 takes a String and modifies it into a Map, which can be passed to the DAO. We'll see how Boon lives up to its name here, because the String argument will be a bit of JSON from the UI. Listing 8 has the controller updates.

Listing 8. Controller converts a JSON String to a Java Map



import java.util.HashMap;

import java.util.Map;

import org.boon.json.JsonFactory;

import org.boon.json.ObjectMapper;

import org.mtyson.dao.DAO;

public class Controller {

	private DAO dao;

	ObjectMapper mapper =  JsonFactory.create(); // 1

	public Controller(DAO dao) {

		super();

		this.dao = dao;

	}

	public String addPerson(String json){

		Map<String,Object> data =  mapper.readValue(json, Map.class); // 2

		if (dao.addPerson(data)){ // 3

			return "{\"message\":\"Added a person!\"}"; 

		} else {

			return "{\"message\":\"Failed to add a person\"}";

		}

	}

}



The line marked 1 creates a mapper that we can use to convert JSON (it's a class member -- this ObjectMapper is designed to be reused). The line marked 2 uses the mapper to parse the string into a Java Map. Finally, in line 3, the map is passed into the DAO.

Now if we send a POST request with the body, our new Person will be added to the database. Remember that the primary key is an auto-increment field, so that isn't shown.

Listing 9. JSON body for the create Person POST



{"first_name":"David","last_name":"Gilmour"}



Here's the request displayed in Postman:

javamicroframeworksp3 fig1 
Figure 1. Creating a Person from Postman. Credit: Matthew Tyson 

The statically typed data layer

So far I've demonstrated a dynamically typed approach to creating the Spark data layer, modeling with maps of data rather than explicitly defined classes. If we wanted to push further in the dynamic direction, we could insert a single add(String type, Map data) method in the DAO, which would programmatically persist a given type. For this approach we'd need to write a layer to map from Java to SQL types.

The more common approach to persistence is to use model classes, so let's take a quick look at how that would work in Spark. Then we'll wrap up the remaining Person CRUD.

Persistence with a model class

For a more traditional, statically typed approach to the data layer, we start by adding a Person class to the original stub application, as seen in Listing 10. This will be our model.

 

Previous Page  1  2  3  4  5  6  7  8  Next Page 

Sign up for CIO Asia eNewsletters.