Skip to content

Library1: API Rest Book CRUD with Postman Swagger

Albert edited this page May 26, 2022 · 1 revision

Welcome to the cifojava2022-3 wiki!

Spring Boot Projects: Library1

Base project

  • 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

Versions

  • 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 with Headers 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
        <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(); } }

Clone this wiki locally