swagger3 spring boot

官网:https://swagger.io/

快速接入

1、pom文件加入依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

2、摘要相关配置

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo())
                // 是否开启
                .enable(true).select()
                // 扫描的路径包
                .apis(RequestHandlerSelectors.basePackage("com.llgcollector.controller"))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

     private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot-Swagger3集成示例")
                .description("springboot | swagger")
                .contact(new Contact("test", "https://www.test.com", "test@163.com"))
                .version("1.0.0")
                .build();
    }

}

3、编写controller

@Api(tags = "测试类",value = "Test Controller")
@RestController
@RequestMapping("/test")
public class TestController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);

    @Resource
    private TestRepo testRepo;

    @ApiOperation(value = "获取信息",notes = "get Message")
    @RequestMapping(value = "/getMessage", method = RequestMethod.GET)
    public ApiResponse getMessage(@ApiParam(name = "id",value = "主键",required = true) Long id) {
        return new ApiResponse<>(testRepo.getById(id));
    }
}

4、开启Swagger

@EnableOpenApi
@SpringBootApplication
public class Application {...

5、浏览器查看效果
https://127.0.0.1:8081/swagger-ui/index.html
在这里插入图片描述

6、如果加了拦截器继承了WebMvcConfigurationSupport会导致swagger失效,此时可以将继承WebMvcConfigurationSupport改成实现WebMvcConfigurer接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值