Spring REST training
Verb | Action | Should be Idempotent (always same result) |
Should be safe (don’t change resources) |
---|---|---|---|
GET | fetch an existing resource |
yes | yes |
POST | create a new resource |
no | no |
PUT | update an existing resource |
yes | no |
DELETE | delete an existing resource |
yes | no |
HEAD | fetch headers of an existing resource |
yes | yes |
OPTIONS | fetch methods of an existing resource |
yes | yes |
PATCH | modify an existing resource |
no | no |
https://www.restapitutorial.com/httpstatuscodes.html
https://start.spring.io/ → create spring boot project from scratch.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
package com.adiaz;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private static final Contact CONTACT = new Contact ("a_name", "http://www.contact_url.org", "contact@email.com");
private static final ApiInfo MY_API_INFO = new ApiInfo("My awesonme API Documentation",
"This is an getting started proof of concept API", "1.0.1",
"https://en.wikipedia.org/wiki/MIT_License",
CONTACT, "MIT", "https://opensource.org/licenses/MIT");
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(MY_API_INFO);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Swagger Tools
YAML: http://www.yamllint.com/
dreed api-description-hellobean.yml http://localhost:8080
swagger: "2.0"
info:
version: "1.0"
title: "Example API"
basePath: /
schemes:
- http
paths:
/helloBean:
get:
produces:
- application/json;charset=utf-8
responses:
'200':
description: 'OK'
schema:
type: object
properties:
message:
type: string
required:
- message
Add links with resources to response.
Example:
{
"id": 2,
"name": "antonio",
"birthDate": "2019-02-27T14:20:22.994+0000",
"_links": {
"all-users": {
"href": "http://localhost:8080/users"
}
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
@GetMapping("/users/{id}")
public Resource<User> usersRetrieve(@PathVariable int id) throws UserNotFoundException {
User user = serviceUser.findUser(id);
if (user==null) {
throw new UserNotFoundException("No existe listillo");
}
Resource<User> resource = new Resource<>(user);
ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).usersRetrieve());
resource.add(linkTo.withRel("all-users"));
return resource;
}