SpringBoot中Swagger的构建和遇见坑

一、构建swagger UI文档

1、加入依赖


    org.springframework.boot
    spring-boot-starter-parent
    2.1.0.RELEASE
     
  

  
    UTF-8
    UTF-8
    1.8
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-data-jpa
    
    
      mysql
      mysql-connector-java
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
    
      org.projectlombok
      lombok
      provided
    
    
    
      com.spring4all
      swagger-spring-boot-starter
      1.9.0.RELEASE
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

基于1.9.0.RELEASE的swagger要求配置好基本信息才能使用swagger文档,否则会有Basic Error Controller的错误,在application.yml配置如下:

swagger:
  base-path: /**
  title: "接口文档"
  description: "接口文档描述"
  version: 1.9.0.RELEASE
  license: Apache License, Version 2.0
  licenseUrl: https://www.apache.org/licenses/LICENSE-2.0.html
  contact:
    name: zhongbao
    url: https://blog.csdn.net/Haihao_micro
    email: *************@qq.com
  base-package: com.zbz   

Application的加上@EnableSwagger2Doc开启使用

@EnableSwagger2Doc
@SpringBootApplication(scanBasePackages = "com.zbz")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

在com.zbz下使用@Api(tags = “用户/管理”)注解UserController

@Api(tags = "用户/管理")
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserDao userDao;

    @GetMapping("/")
    @ApiOperation(value = "获取用户列表")
    public List getUserList() {
        List r = userDao.findAll();
        return r;
    }
}

以上配置完毕,一个swagger文档就可以访问使用,http://localhost:8080/swagger-ui.html

SpringBoot中Swagger的构建和遇见坑_第1张图片

你可能感兴趣的:(JavaEE)