Spring Boot – Creating a RESTful Web Service Example

Spring-Boot-Tutorials » on Aug 27, 2017 { 17 Comments } By Sivateja

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

  • pom.xml


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootApp {
	public static void main(String[] args) 
        {, args);


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

public class SpringJava4sController {

	public String welcome() {
		return "Welcome to Spring Boot Tutorials";
	public String myData() {
		return "Hello Spring Boot";


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 and controller class in, 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 package, while starting our application, it will scan all the components under that package.  As we have created our controller class in which is inside, 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..


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

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 .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


17 Responses to “Spring Boot – Creating a RESTful Web Service Example”
  1. Chetan bhandari says:

    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.

  2. Venkatesh says:

    Hi sir,

    Can u provide spring+rest services+hibernate CRUD operation.

  3. Chetan bhandari says:


  4. Jayaram says:

    really java4s is very helpful sir,I learnt a lot from you.

  5. vinay says:

    Excellent. Thank You Bro

  6. Vinaykumar says:

    I'm new to spring boot. Yours way of explaining the concept its good.

  7. Anjali says:

    Thank you so much Sivateja.

  8. Kalaiselvan Velmurugan says:

    Cute Explanation. I enjoyed very well with your words. I read your article like bed time story

  9. Sheena says:

    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…

  10. Nahendra says:

    All technologies your explanation is awesome.

  11. Gayathri says:

    Your explanation is very neat, clean and understandable…Your knowledge sharing is really helping me and to so many thank you sir

  12. Nirmal kumar says:

    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….

  13. Perumalsamy says:

    Excellent explanation. Thanks for sharing knowledge.

  14. Jitendra Bonde says:

    Thank You Bro

  15. Devendra Konnur says:

    Very nice explanation.. your article is completely self explanatory… Thanks for sharing your knowledge.

  16. Mohan Kumar says:

    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


  17. Shivani Goyal says:

    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.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.