Library1: API Rest Book CRUD with Postman Swagger - AlbertProfe/cifojava2022-3 GitHub Wiki
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
CRUDoperations: Create, Read, Update, Delete -
- By deriving the query from the method name directly.
- By using a manually defined query.
-
Optionalcontainer: 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 inRestTemplateas well as in@Controllermethods. -
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-
OptionalContainer - @RequestBody Book book
- @PostMapping(path="addBook", consumes = "application/JSON") and others
-
-
version 1.2 : CRUD
operation Delete,Optional,ResponseEntityand others- @DeleteMapping("deleteBook")
- @RequestParam Long id
ResponseEntity.accepted().body(bookFound.get());
-
version 1.3 : CRUD
operation Updatetitle,Optional,ResponseEntitywithHeadersand others- @PutMapping("/updateBook/{id}")
- @PathVariable Long id
HttpHeaders headers = new HttpHeaders();
-
version 1.4 : add
Swaggerto 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@Configurationpublic class SpringFoxConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}}