Training - REST & Spring Boot
Last Updated on: August 11, 2021 pm
REST & Spring Boot
Spring Enterprise Web Architecture
REST
Introducing REST
REST stands for Representational State Transfer
REST is a style of architecture
- The Web is a large set of resources
- As a client selects a resource, they are then in a particular state
- The client can then select a different resource, and be in a different state as they now have a different resource
Resources
A resource always has a simple URL
At the end of the URL will be URLs for other resources allowing you to get more information (and therefore change state)
URLs are logical names for resources.
The actual location should be resolved by technology on the server
- Such as a Spring MVC Controller
REST and Web Services
- Clients send HTTP requests using the four HTTP methods
- Simple parameters can be passed in the URL
- Complex parameters are passed in the header as XML or JSON
The Richardson Maturity Model
Spring RESTful Service
Controller
GET All Person
1 |
|
Controller methods are annotated using @RequestMapping to specify
- Which URL (in this case /people as this was placed on the whole class)
- Which HTTP method (in this case GET)
Find Person by Id
Use @PathVariable
1 |
|
http://localhost:8080/people/3
Query String Parameters
1 |
|
http://localhost:8080/people?id=3
Create a Person
@PostMapping(consumes=”..”)
@PostMapping(consumes="application/json", produce="application/json")
- The
consumes
attribute specifies the data type of the data being submitted, defaulting to application/json - The
produce
attribute specifies the MIME type of the return value in our case, defaulting to application/json
@RequestBody
1 |
|
The @RequestBody attribute means that the JSON submitted will be automatically marshalled into a Person object
How to Return a 404
Use a ResponseEntity return type, and set its status
1 |
|
Spring Java Configuration
1 |
|
The WebMvcConfigurerAdapter class provides methods that can be overridden to change specific Web settings such as resource folder locations
Override the addResourcesHandlers
method
1 |
|
Loading Config Class
AbstractAnnotationConfigDispatcherServletInitializer
- Extend the abstract base class
- Override some required methods
- Specify servlet mapping and location of your app config
1 |
|
CORS Configuration
Cross Origin Resource Sharing (CORS) allows you to configure whether web pages from sites other than your own can invoke your REST API
By default, browsers will prevent Web sites calling REST APIs on other servers unless the server specifically allows it
- Allow it is achieved by a header set on the server
Enabling CORS in Spring
Annotate the request methods for REST Controller - @CrossOrigin
1 |
|
Open API/Swagger
Open API
Open API is a Specification format for REST APIs. An OpenAPI file includes:
- Available Endpoints and operations to each endpoint.
- Operation parameters for both input and output.
- Authentication methods.
- Contact information/license/terms of conditions.
Swagger
Swagger is a set of tools built around OpenAPI Specification.
- Swagger UI - renders OpenAPI specs without having any implementation logic.
- Swagger Editor - browser-based editor where you can write OpenAPI specs.
Add Dependency and Annotation
1 |
|
SwaggerConfig File
Enables @EnablesSwagger2
in SwaggerConfig.java
.
1 |
|
Add Annotations to AppConfig.java and Controllers
Enables @Import(SwaggerConfig.class)
in AppConfig.java
.
1 |
|
More annotations like @ApiOperation
can be placed in the controller.
1 |
|