SpringBoot Swagger

1.maven 依赖引入

        <!-- Swagger API文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-ui -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-ui</artifactId>
            <version>3.0.3</version>
        </dependency>

2.配置

  配置一下内容:

    1.扫描的包及仅显示@ApiOperation的类

    2.swagger-ui访问路径设置

package com.example.demo;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))//
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("平台API接口文档")
                .version("1.0")
                .description("后台接口")
                .license("license")
                .licenseUrl("license_url")
                .build();
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }
}

 

3.配置文件 application.yml

swagger:
  enable: true

   

4.访问路径如下

Doc文档 :  http://localhost:8081/doc.html
Swagger-ui:  http://localhost:8081/swagger-ui/index.html

5.项目中使用

@Api(tags="测试")
@RestController
@RequestMapping("/test")
public class Test {

    // 测试而已,没有创建service层
    @Autowired
    private UserMapper mapper;

    @ApiOperation(value = "查询测试")
    @PostMapping("/getUserList")
    public Result<?> getUserList(@RequestBody User usr){
        try {
            LambdaQueryWrapper<User> lambda = Wrappers.<User>query().lambda();
            if(StringUtils.isNotBlank(usr.getName())) lambda.likeRight(User::getName, usr.getName());
            return Result.OK(mapper.selectList(lambda));
        }catch (Exception e){
            return Result.error("获取数据失败");
        }

    }

    @ApiOperation(value = "查询测试-A")
    @PostMapping("/getUserListA")
    public Result<List<User>> getUserListA(@RequestParam(required=false,name = "usrNm") String name){
        try {
            LambdaQueryWrapper<User> lambda = Wrappers.<User>query().lambda();
            if(StringUtils.isNotBlank(name)) lambda.like(User::getName, name);
            return Result.OK(mapper.selectList(lambda));
        }catch (Exception e){
            return Result.error("获取数据失败");
        }
    }
}

效果如下:

SpringBoot Swagger

 

 

 

SpringBoot Swagger

 

上一篇:Spring boot集成Swagger


下一篇:Swagger配置扫描接口以及开关