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
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.
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.
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.
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
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.
Let’s check the output in the console.
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.
Method with @PathVariable
Now, let me modify the controller by adding a path variable. The modified controller is given below.
This annotation specifies that the method parameter should be bound to a URI template variable. In our controller
username parameter is bound to
Now let’s save and relaunch the application. The tomcat server gets restarted.
Once done, invoke the service in the browser, as
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.
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
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]
Till then, Happy Learning