Sonntag, 22. Januar 2012

Spring Roo Rest Service with JSON Interface

Here are the steps to build up a REST Service with Spring Roo from the scratch! This Service is accessible via JSON requests.

I did this on my ubuntu 11.10 system. Open JDK and Sun Java were already installed. 

  1. Java Enrivonment
    - Sun Java is installed by default at /usr/lib/jvm/java-6-sun-1.6.0.26/
    - I moved the /usr/bin/java , which was pointing to the other java installation ( open JDK ), to /usr/bin/javaopen
    - renewed /usr/bin/java by:
    > sudo ln -s /usr/lib/jvm/java-6-sun-1.6.0.26/bin/java /usr/bin/java
    > sudo export JAVA_HOME= /usr/lib/jvm/java-6-sun-1.6.0.26
  2. Install Maven2
    > sudo apt-get install maven2
  3. Install Spring Roo ( Version 1.2.0 )
    - Download spring-roo.zip from springsource site and unzip it somewhere. (let's call it $ROO_HOME)
    > sudo ln -s $ROO_HOME/roo.sh /usr/bin/roo

  4. Setup Roo Project> mkdir myrooproject
    > cd myrooproject
    > roo
    roo> project --topLevelPackage org.klarblick
    roo> hint
    roo> jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
    roo> hint
    roo> entity jpa --class ~.domain.Note
    roo> hint
    roo> field string note
    roo> json all
    roo> web mvc setup
    roo> controller all --package ~.controller
    roo> web mvc json all
    roo> quit
  5. Start Roo Project
    > mvn tomcat:run
  6. Test Roo Project
    - browse to http://localhost:8080/klarblick and create a new Note
    - get a REST Client for your browser
    - JSON GET Request on: http://localhost:8080/klarblick/note/1
    Don't forget the request header: Accept: application/json

3 Kommentare:

  1. Hi!
    On step 4
    #web mvn json all
    should it say:
    #web mvc json all

    First one doesn't work, second one at least get's accepted and processed by roo shell

    AntwortenLöschen
  2. If you want the service alone without interface spring roo doo a nice job too

    roo> project --topLevelPackage org.klarblick
    roo> hint
    roo> jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
    roo> hint
    roo> entity jpa --class ~.domain.Note
    roo> hint
    roo> field string note
    roo> json add
    roo> web mvc json setup
    roo> web mvc json all

    AntwortenLöschen