Show Topics
Hide Topics

Spring Boot RESTful Web Service Example


To work with webservices in SpringBoot we have to use two annotations

  • @RestController: tells Spring Boot to consider this class as REST controller
  • @RequestMapping: used to register paths inside it to respond to the HTTP requests.


The @RestController is a stereotype annotation. It adds @Controller and @ResponseBody annotations to the class.

@RestController = @Controller + @ResponseBody


Note – The @RestController and @RequestMapping annotations are Spring MVC annotations. They are not specific to Spring Boot.



Create pom.xml same as first example.



Test the Application

Right click on project > Run as >Java Application >select SpringBootApp


  • In above Spring Boot main application class in app package and controller class in controller. While starting our application, SpringBootApp class will scan all the components under that package.  As we have created our controller class in app.controller which is inside app package, our controller was registered by spring boot.


  • If you create the controller class outside of the main package, lets say com.smlcodes.controllerIf you run the application it gives 404 error.To resolve this,  we have to add @ComponentScan annotation in our Spring Boot main class, as below
Download Example