Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 2: Ninja

Matthew Tyson | Nov. 25, 2015
Build a functional Person application with Ninja.

Selecting an environment

Ninja allows you to select the configuration values for different environments. Supported environments are dev, prod, and test. Left to default, the environment resolves to production. You can select all configuration values with this mechanism, including things like the persistence unit and logging, which I'll demonstrate in a bit.

As an example, you could create different persistence units in your persistence.xml and reference them in application.conf, as shown below. Notice the %<environment name> notation for referencing the environments.



database.name=database_production   # will be used when no mode is set (or prod)

%prod.database.name=database_prod   # will be used when running in prod mode

%dev.database.name=database_dev     # will be used when running in dev mode

%test.database.name=database_test   # will be used when running in test mode



The last piece of the puzzle is to set the active environment. If you don't set it yourself, it will resolve to prod; otherwise, you can set the ninja.mode system property to select any of the supported environments.

Ninja config

Next we'll connect our persistence model to Ninja. Open the /src/main/java/confg/application.conf file and append Listing 3.

Listing 3. Additions to /src/main/java/confg/application.conf



# dev db mysql

db.connection.url=jdbc:mysql://localhost/ninja_app

db.connection.username=root

db.connection.password=password

# Active connection

ninja.jpa.persistence_unit_name=devdb

Here we are telling Ninja what URL, username, and password to use, along with the persistence_unit to use. Remember that we defined the persistence unit with devdb in Listing 2.

Next we'll save a Person. We'll begin by mapping a URL, which will be a REST endpoint. URL mapping in Ninja happens in the Routes.java file, as shown in Listing 4.

Listing 4. Adding a Person POST endpoint to src/main/java/conf/Routes.java


router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController.class, "serveWebJars");
router.GET().route("/assets/{fileName: .*}").with(AssetsController.class, "serveStatic");
//...

router.POST().route("/person").with(ApplicationController.class, "createPerson"); // Add this line!

In Part 1 we developed a simple ApplicationController. Now we'll revisit that in order to add a createPerson() method. This will give us a view into how Ninja interfaces with the JPA infrastructure. Listing 5 has the additions for the controller.

Listing 5. Additions to /src/main/java/controllers/Application.java



package controllers;

import javax.persistence.EntityManager;

import models.Person;

//...

import com.google.inject.Inject;

import com.google.inject.Provider;

import com.google.inject.Singleton;

import com.google.inject.persist.Transactional;

@Singleton

public class ApplicationController {

	@Inject

	Provider<EntityManager> entitiyManagerProvider;

	@Inject

	Router router;

	@Transactional

	public Result createPerson(Person person){

		EntityManager entityManager = entitiyManagerProvider.get();

		entityManager.persist(person);

		return Results.json().render("{}");

	}

//...

Recall that Ninja uses Guice for dependency injection and persistence management. Here you can see it in action. Note the two @Inject annotations, which let us wire in objects by type: First, we inject Provider<EntityManager>, which provides an implementation of javax.persistence.EntityManager -- the main entry point for Guice's persistence support. Second, we inject a Router, which will let us take advantage of Ninja's programmatic routing.

Finally, take a look at the createPerson() method. The @Transactional annotation is very important! It tells Guice that the method should be instrumented for persistence. Without it, nothing interesting will happen. Our next task is to get an instance of EntityManager and call persist() on our Person object.

 

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

Sign up for CIO Asia eNewsletters.