Developing a RESTful service – the legacy approach

This example will review the traditional RESTful service development before jumping deep into Spring Boot.

STS will be used to develop this REST/JSON service.

Note

The full source code of this example is available as the legacyrest project in the code files of this book.

The following are the steps to develop the first RESTful service:

  1. Start STS and set a workspace of choice for this project.
  2. Navigate to File | New | Project.
  3. Select Spring Legacy Project as shown in the following screenshot and click on Next:
    Developing a RESTful service – the legacy approach
  4. Select Spring MVC Project as shown in the following diagram and click on Next:
    Developing a RESTful service – the legacy approach
  5. Select a top-level package name of choice. This example uses org.rvslab.chapter2.legacyrest as the top-level package.
  6. Then, click on Finish.
  7. This will create a project in the STS workspace with the name legacyrest.

    Before proceeding further, pom.xml needs editing.

  8. Change the Spring version to 4.2.6.RELEASE, as follows:
    <org.springframework-version>4.2.6.RELEASE</org.springframework-version>
  9. Add Jackson dependencies in the pom.xml file for JSON-to-POJO and POJO-to-JSON conversions. Note that the 2.*.* version is used to ensure compatibility with Spring 4.
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.4</version>
    </dependency>
  10. Some Java code needs to be added. In Java Resources, under legacyrest, expand the package and open the default HomeController.java file:
    Developing a RESTful service – the legacy approach
  11. The default implementation is targeted more towards the MVC project. Rewriting HomeController.java to return a JSON value in response to the REST call will do the trick. The resulting HomeController.java file will look similar to the following:
    @RestController
    public class HomeController {
      @RequestMapping("/")
      public Greet sayHello(){
        return new Greet("Hello World!");
      }
    }
    class Greet { 
      private String message;
      public Greet(String message) {
        this.message = message;
      }
      //add getter and setter
    }

    Examining the code, there are now two classes:

    • Greet: This is a simple Java class with getters and setters to represent a data object. There is only one attribute in the Greet class, which is message.
    • HomeController.java: This is nothing but a Spring controller REST endpoint to handle HTTP requests.

    Note that the annotation used in HomeController is @RestController, which automatically injects @Controller and @ResponseBody and has the same effect as the following code:

    @Controller
    @ResponseBody
    public class HomeController { }
  12. The project can now be run by right-clicking on legacyrest, navigating to Run As | Run On Server, and then selecting the default server (Pivotal tc Server Developer Edition v3.1) that comes along with STS.

    This should automatically start the server and deploy the web application on the TC server.

    If the server started properly, the following message will appear in the console:

    INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 906 ms
    May 08, 2016 8:22:48 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 2289 ms
    
  13. If everything is fine, STS will open a browser window to http://localhost:8080/legacyrest/ and display the JSON object as shown in the browser. Right-click on and navigate to legacyrest | Properties | Web Project Settings and review Context Root to identify the context root of the web application:
    Developing a RESTful service – the legacy approach

The alternate build option is to use Maven. Right-click on the project and navigate to Run As | Maven install. This will generate chapter2-1.0.0-BUILD-SNAPSHOT.war under the target folder. This war is deployable in any servlet container such as Tomcat, JBoss, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.156.50