3

Building a Spring RESTful Service using Spring Boot

[Social9_Share type="horizontal"]

Part-II: Building a Spring RESTful Service using Spring Boot

In this post, I am converting an existing spring appilcation into spring RESTful service using Spring Boot. You can do the necessary setup and configuration needed for spring boot and learn a basic example in Part I. The changes to be done are

  • Add dependency for web in pom.xml
  • Create a controller and annotate with @RestController
  • Package and run as a JAR file
  • Run the service from the browser
  • Method with @PathVariable
  • Response as JSON representation

Add dependency for web in pom.xml

Now , in the same project, let’s add the below given dependency for web in pom.xml.

All the relevant jars and the transitive dependencies will be added now. If you notice, all relevant dependencies for Spring Boot, Spring jars, Jackson jars, Hibernate validator jars, Embedded Tomcat, logging are automatically added without any version problem. You can see the modified pom below.

springboot-pom1

Create a controller class

Next, let us add a web controller class with the name HomeController

This class is annotated with @RestController. Lets discuss the annotation one by one.

@RestController

The HTTP requests are handled by a controller. This annotation is equivalent to adding @Controller and @ResponseBody . It has an implicit @ResponseBody for all methods that are annotated with @RequestMapping. In a class that is annotated with @RestController, every method returns a domain object rather than a view.

@RequestMapping

This annotation provides the routing details . It is used to map the requests to the specific handler methods using the path specified. In this controller “/home” is mapped to greetMessage() method.

@ResponseBody

If a method is annotated with @ResponseBody, the return value of the method will be provided to the user as HTTP Response.

Package and run as JAR

All done. This is now behaving like a web application, but packaged as jar. There is no web.xml needed here and also no plumbing of code.You can open this web application, running as a REST service from the browser as well. Lets look into the main class.

No changes in the main class. Now, lets run the application.Select your project or main class and run as SpringBootApp (If STS integrated in eclipse) or as java application.

springboot-run1

Let’s check the output in the console.

springboot-con1

These are the observations.

  • The embedded Tomcat Server is started automatically and is running in port 8080.
  • The WebApplicationContext of spring is initialized.
  • The path “/” is mapped to the Spring DispatcherServlet
  • The path “/home” is mapped to the method greetMessage() method and few other mappings also are there.

Now, the service is ready to be invoked.

Run the service from the browser

To run the service in the browser, open the browser and type http://localhost:8080/home. You can see the output in the browser.

springboot-brow1

Method with @PathVariable

Now, let me modify the controller by adding a path variable. The modified controller is given below.

@PathVariable

This annotation specifies that the method parameter should be bound to a URI template variable. In our controller username parameter is bound to name pathvariable.
Now let’s save and relaunch the application. The tomcat server gets restarted.

springboot-launch

Once done, invoke the service in the browser, as http://localhost:8080/home/Priya

springboot-brow2

Response as JSON representation

Let us add a domain class and make the RestService controller return a domain object as JSON. So, let us create a simple domain class Book with title, author and price as given below

Next, let us create a BookController class, having a method that returns a book object as response. No need to add @Component annotation as the @SpringBootApplication annotation (refer Part -I) scans all the classes and identifies them as spring bean. But if the package structure is in a different hierarchy from the main class then the main class must be annotated with @ComponentScan.The Book Controller class is given below.

In this controller class, I am creating a book object, populating it with values and returning this as a response.

Spring uses JACKSON JSON library(added to build path by spring boot)to automatically marshal book instances to JSON representation. The Class MappingJackson2HttpMessageConverter converts the book object to JSON.

Now, let’s relaunch the application as Java Application or SpringBootApp. The server is started and service is ready to be invoked. Open the browser and type http://localhost:8080/bookDetails

springboot-brow3

You can download the complete project here SpringBootDemo1.zip
[This is a safe ZIP file. you may get warnings saying that it is dangerous. but you can download and extract this file. you will get the complete project.Import this as Maven project in eclipse and run the application to get the result]

You can also take the jar file and run the application from the command prompt(just as showed in Part I). In Part III, learn how to package the same application as war file and run it in the browser.

Till then, Happy Learning

Post a comment