Monday, December 12, 2011

Adding JSON support to domain objects and controllers

This post shows how you can add JSON support to your applications using Spring Roo. The information in this post has been taken from Spring Roo 1.1 Cookbook.

Step 1: Download Roo scripts and sample code from the following location:http://code.google.com/p/spring-roo-cookbook/downloads/list#columnprefs

Step 2: Execute the ch04_web-app.roo script that creates the flight-app Roo project, sets up
Hibernate as the persistence provider, configures MySQL as the database for the application,
creates the Flight and FlightDescription JPA entities, and defines a many-to-one
relationship between the Flight and FlightDescription entities. If you are using a
different database than MySQL or your connection settings are different than what is specified
in the script, then modify the script accordingly.

Step 3: Execute the controller all command to create controllers and JSPX views corresponding
to JPA entities in the flight-app project, as shown here:
.. roo> controller all --package ~.web
Execute the perform eclipse command to update the project's classpath settings, as
shown here:
.. roo> perform eclipse
Now, import the flight-app project into your Eclipse IDE.

Step 4: To add the json support execute the json add command against the Flight JPA entity:
~.domain.Flight roo> json add --class ~.domain.Flight
Updated SRC_MAIN_JAVA\...\domain\Flight.java
Created SRC_MAIN_JAVA\...\domain\Flight_Roo_Json.aj
Created SRC_MAIN_JAVA\...\web\FlightController_Roo_Controller_Json.aj

Executing the json add command creates
a *_Roo_Json.aj AspectJ ITD, which defines methods for converting objects of the class to
JSON documents and vice versa, as shown here:

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

privileged aspect Flight_Roo_Json {
  public String Flight.toJson() {
    return new JSONSerializer().exclude("*.class").serialize(this);
  }
  public static Flight Flight.fromJsonToFlight(String json) {
    return new JSONDeserializer().use(null, Flight.class).deserialize(json);
  }
  public static String Flight.toJsonArray(
    Collection collection) {
    ...
  }
  public static Collection
    Flight.fromJsonArrayToFlights(String json) {
     ...
  }
}

The following code shows the FlightController_Roo_Controller_Json.aj ITD, which
was generated:
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ResponseBody;

privileged aspect FlightController_Roo_Controller_Json {

   @RequestMapping(value = "/{flightId}", method = RequestMethod.GET, 
     headers =  "Accept=application/json")
  @ResponseBody
  public Object FlightController.showJson(@PathVariable("flightId") Long flightId) {
    Flight flight = Flight.findFlight(flightId);
    if (flight == null) {
         HttpHeaders headers= new HttpHeaders();
         headers.add("Content-Type", "application/text");
         return new ResponseEntity(headers, HttpStatus.NOT_FOUND);
    }
    return flight.toJson();
  }
  ...
}