Newsletter |
Spring Boot – Creating a RESTful Web Service Example
In the previous article we have just created a simple hello world spring boot application, in this tutorial I am going to show you how to create a Restful web service using Spring Boot, believe me its very simple 🙂
Lets start with the directory structure of the project. (if you want to know how to create a simple spring boot project, you can go back to the previous article Spring Boot + Maven – Hello World Example Step by Step)
Directory Structure
 Required files
- SpringBootApp.java
- SpringJava4sController.java
- pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4s</groupId> <artifactId>SpringBootHelloWorld</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> </project>
SpringBootApp.java
package com.java4s.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootApp { public static void main(String[] args) { SpringApplication.run(SpringBootApp.class, args); } }
SpringJava4sController.java
package com.java4s.app.controller; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class SpringJava4sController { @RequestMapping("/") public String welcome() { return "Welcome to Spring Boot Tutorials"; } @RequestMapping("/hello") public String myData() { return "Hello Spring Boot"; } }
Explanation
Friends, I took previous spring boot hello world application and just added the SpringJava4sController class and written RESTful web service related logic. I haven’t added any new dependencies nor written any XML’s, rather added a simple java class. My REST class is annotated with @RestController, which tells Spring Boot to consider this class as REST controller and register @RequestMapping paths inside it to respond to the HTTP requests.
Note: Spring Boot will use 8080 as default tomcat port
Run the application and hit http://localhost:8080 Â [Â This is for @RequestMapping(“/”) ]
Now try hitting  http://localhost:8080/hello  [ This is for @RequestMapping(“/hello”) ]
Note:  Have you clearly observed the above directory structure? I have created Spring Boot main application class in com.java4s.app and controller class in com.java4s.app.controller, and in my controller class I have written my RESTful service logic and was able to execute the application successfully. How spring boot knows to scan our controller? As we have created our main class in com.java4s.app package, while starting our application, it will scan all the components under that package.  As we have created our controller class in com.java4s.app.controller which is inside com.java4s.app, our controller was registered by spring boot.
If you create the controller class outside of the main package, lets say com.java4s.controller, If you run the application it gives 404 error, just give a try and see 🙂
What’s the solution for this?  we have to add @ComponentScan annotation in our Spring Boot main class, something like this..
package com.java4s.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages="com.java4s.controller") public class SpringBootApp { public static void main(String[] args){ --- } }
That’s it friends, hope you enjoyed this article 🙂
You Might Also Like
::. About the Author .:: | ||
Hi Sir,
Your way of explanation is too good, i always refer this side to recall any toping which has been discuss in this turorial.
I have request you to provide detail tutorial with more example on Spring Rest, Sprint Boot.
And also please provide some Java 8 Concepts.
Hi sir,
Can u provide spring+rest services+hibernate CRUD operation.
Could you please provide some SPRING REST+SPRING BOOT+ANGULAR COMBINATION EXAMPLE IN DETAIL
really java4s is very helpful sir,I learnt a lot from you.
Excellent. Thank You Bro
I'm new to spring boot. Yours way of explaining the concept its good.
Excellent.
Thank you so much Sivateja.
Cute Explanation. I enjoyed very well with your words. I read your article like bed time story
Your explanation is awesome.. I am learning all technolgies which I am new to me is googling thru java4s only..Its clear and straight forward…Easily understand your way of expalantion.. Keep updating your portal with all new trends in java…
All technologies your explanation is awesome.
Your explanation is very neat, clean and understandable…Your knowledge sharing is really helping me and to so many thank you sir
Hiii sir your explanation is very excellent thank you for giving this wonderfull tutorials and i would like to request you please make youtube videos it's so helpfull for us….
Excellent explanation. Thanks for sharing knowledge.
Thank You Bro
Very nice explanation.. your article is completely self explanatory… Thanks for sharing your knowledge.
i am getting the following error while running main class
"
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 23 18:57:28 IST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
"
Hello sir,
i follow your code but when add @componentScan annotation still i'm getting output, and when i'm deleting annotation still getting output. Please help me.