-
Notifications
You must be signed in to change notification settings - Fork 6
Library1: API Rest Book CRUD with Postman Swagger
Welcome to the cifojava2022-3 wiki!
- Base project:
- POM
- API REST Read CRUD
- DataBase H2
- Application.properties
- Command Line Runner with methods to test
- Postman to test API REST, Postman web
- @Entity, @RestController, @Service, @CrudRepository JPA 2.0, @Component
-
New tools:
-
All
CRUD
operations: Create, Read, Update, Delete -
- By deriving the query from the method name directly.
- By using a manually defined query.
-
Optional
container: A container object which may or may not contain a non-null value. If a value is present,isPresent()
will return true andget()
will return the value. -
ResponseEntity
: Extension of HttpEntity that adds an HttpStatus status code. Used inRestTemplate
as well as in@Controller
methods. -
Annotations
:- @RestController
- @RequestMapping("api")
- @Autowired
- @GetMapping("books")
- @PostMapping(path = "addBook", consumes = "application/json" )
- @RequestBody Book book
- @DeleteMapping ("/deleteBook")
- @PathVariable Long id
- @PostMapping("/replaceBook/{id}")
-
Operations CRUD:
- CRUD:
-
Endpoints:
-
-
UPDATE: http://localhost:8080/api/updateBook/90
-
version 1.0 : CRUD
operation Read
, basic project -
version 1.1 : CRUD
operation Create
-
Optional
Container - @RequestBody Book book
- @PostMapping(path="addBook", consumes = "application/JSON") and others
-
-
version 1.2 : CRUD
operation Delete
,Optional
,ResponseEntity
and others- @DeleteMapping("deleteBook")
- @RequestParam Long id
ResponseEntity.accepted().body(bookFound.get());
-
version 1.3 : CRUD
operation Update
title,Optional
,ResponseEntity
withHeaders
and others- @PutMapping("/updateBook/{id}")
- @PathVariable Long id
HttpHeaders headers = new HttpHeaders();
-
version 1.4 : add
Swagger
to project, swagger web and refactor-
URL swagger :
http://localhost:8080/swagger-ui.html
- Java Version and dependencies
-
URL swagger :
<properties> <java.version>11</java.version> </properties>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
-
Annotation: @EnableSwagger2 in Main
-
application.properties:
> spring.mvc.pathmatch.matching-strategy=ant-path-matcher
-
java class config:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
}
by Java Cifo 2022 IFCD53 Desenvolupament en Java amb framework Spring