Create a RESTful web services using spring boot.

  1. go to SpringInitilizr to generate a spring boot maven project with following dependencies - web, devtools, jpa, h2, actuator etc.(groupid is like the name of package structure you want , artifact_id is the name of the project you want).
  2. ZIP of RestfulServicesProject
  3. Unzip and import in preferred IDE.
  4. Generally CRUD operations are mostly used. CRUD stands for Create Read Update Delete. Operations are  like create user (POST), retrieve user (GET), update user (PUT), Delete User (DELETE).
  5. Uri's examples, /users, /users/{id}, /users/posts/1.
  6. Use annotations like @Controller (/@RestController), @RequestMapping(it will have the method which we are going to use like GET/POST(method = RequestMethod.GET, path = " path of the uri").
  7. With that your can hit the endpoint from a browser and get the response back.
  8. Now, Let us take a step back to understand whats happening in the background - Spring framework will have classes, when you pull in the dependencies and run the spring boot application several beans for these classes (DispatcherServlet , ErrorMvcAutoConfigurations etc..) are created by spring boot auto configuration.
  9. @SpringBootApplication – indicates that this is spring context file. enables ""auto configuration"", enables component scan (scan this package and sub packages).
  10. DispatcherServlet handles all the request, It is also called the front controller.
  11. DispatcherServlet looks for which controller to send the request to and sends it to that bean. Once it gets response it is mapped to @ResponseBody by message converter like jackson which returns the JSON response and then it sends the response back.
  12. Some of important annotations are @PathVariable, @Service, @Controller, @Respository, @Configuration, @Entity, @RequestMapping, @Autowired etc.
  13. Implementation of Basic Auth with Spring security - add spring-boot-starter-security dependency. default is user and password you get it in console logs.

 

USEFUL TIPS