Create a RESTful web services using spring boot.
- 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).
- ZIP of RestfulServicesProject
- Unzip and import in preferred IDE.
- 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).
- Uri's examples, /users, /users/{id}, /users/posts/1.
- 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").
- With that your can hit the endpoint from a browser and get the response back.
- 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.
- @SpringBootApplication – indicates that this is spring context file. enables ""auto configuration"", enables component scan (scan this package and sub packages).
- DispatcherServlet handles all the request, It is also called the front controller.
- 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.
- Some of important annotations are @PathVariable, @Service, @Controller, @Respository, @Configuration, @Entity, @RequestMapping, @Autowired etc.
- 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
- ResponseEntity<T> which is extension of httpEntity is useful for returning status code.
- @ResposeStatus(HttpStatus.NOT_Found) on exception class.
- implement generic Exception handling for all resources.
- Can you javax validation API to perform validations for domain class variables.
- Adhere to HATEOAS concepts.
- Add in dependencies which supports both JSON and XML.
- Configure Auto generation of Swagger Documentation( similar to WSDL in SOAP) - add dependencies like springfox-swagger2, springfox-swagger-ui. write SwaggerConfig class use
- @Configuration, @EnableSwagger2 on class, @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) } then go to localhost:8080/swagger-ui.html
- Monitoring API's - if your application is down i want to know it as soon as possible. In spring this is provided by spring-boot-starter-actuator. also add dependency of spring-data-rest-hal-browser. Hal browser will show all the hal services which are being exposed by spring-boot-starter-actuator (url for actuator- localhost:8080/actuator)(type local:8080 and put in the service which you want to see the details for).
- Use @JsonIgnore to not send the value in the response.