SpringBoot (eight) integrate swagger

Integrate swagger

1. What is swagger

Swagger is a web service for generating, describing, and invoking RESTful interfaces. Derived annotation requests (GetMapping, PostMapping, PutMapping, DeleteMapping) must be used.

In layman's terms, Swagger is a service that displays all (wanted to expose) interfaces in the project ( the interface is the request path of the method , not the interface) on the page, and can perform interface calls and tests . The backend sends the corresponding request)

1. It is a standardized and complete framework that allows you to better write API documents.

2. Provide description, production, consumption and visualization RESTful Web Service.

3. A formal specification supported by a huge collection of tools. This collection covers everything from the end-user interface, the underlying codebase, to business API management

Access path: http://127.0.0.1:8081/swagger-ui.html#/

Two, springboot integrated swagger

1. Import dependencies

<!--springboot集成swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2. Configuration file

@Configuration//标为为配置类,相当于spring.xml
@EnableSwagger2
public class SwagerrConfiguration {
     private static ApiInfo DEFAULT = null;
     @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2);
    }
}

3. Configure the yml file to solve the problem of Failed to start bean 'documentationPluginsBootstrapper' when the high-version SpringBoot integrates Swagger startup error 

spring:
  #解决 高版本SpringBoot整合Swagger 启动报错Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

4. Test

@RestController
@RequestMapping("/test")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/user") //使用 @GetMapping这个派生注解,就是为了测试swagger
    //测试swagger访问这个路径http://localhost:8080/swagger-ui.html
    public List<User> test(Integer pageNum,Integer pageSize){
        PageInfo<User> pageInfo = userService.selectAllName(pageNum, pageSize);
        List<User> list = pageInfo.getList();
        System.out.println("pageInfo:"+pageInfo);
        return list;
    }
    @PostMapping("/testPost")
    public String testPost(User user){
        return user.getName();
    }
    @RequestMapping("view")
    public String view(){
        return "哈哈哈哈哈哈哈哈哈哈哈哈哈";
    }
}

4. Access swagger

http://127.0.0.1:8081/swagger-ui.html#/

You must use @GetMapping("/user") to use the @GetMapping derived annotation request (GetMapping, PostMapping, PutMapping, DeleteMapping), just to test swagger

 

Guess you like

Origin blog.csdn.net/m0_65992672/article/details/130421527