springboot集成swagger3

pom直接引用springfox-boot-starter,swagger3不需要引用ui包。

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

开始相关配置,创建config配置类。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author lhq
 */
@EnableOpenApi
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(buildApiInfo())
                .enable(true)
                .select()
                // 要扫描的API(Controller)基础包
                .apis(RequestHandlerSelectors.basePackage("com.hsrc.fjyc.intelligentassistant.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo buildApiInfo() {
        return new ApiInfoBuilder()
                .title("stoneAPI文档")
                .description("平台管理服务api")
                .version("1.0.0")
                .build();
    }
}

注意:new Docket(DocumentationType.OAS_30)这里不再使DocumentationType.SWAGGER_2,从swagger2切换过来的一定小心,不然可能无法识别相关请求,加载一个空页面。

注解使用:@Tag代替@Api,其中name的值一定是类名小写,类名中间存在大写的用横线间隔开,大写变小写。

/**
 * 会话相关
 * @author lhq
 */
@Tag(description = "会话相关",name = "chatbot-controller")
@RestController
public class ChatbotController {
    private Logger logger= LogManager.getLogger(ChatbotController.class);
    @Resource
    AlibabaUtill alibabaUtill;
    @Resource
    JwtUtil jwtUtil;
    @Operation(summary = "获取热点(大家都在看)")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功",ref = "成功"),
            @ApiResponse(responseCode = "400",description = "失败",ref = "失败")
    })
    @PostMapping("/hotKnowledge")
    public String hotKnowledge(@Parameter(name = "token",description = "调用请求的令牌;",required = true) @RequestParam(value="token")String token,
                           @Parameter(name = "limit",description = "总条数",required = true) @RequestParam(value="limit") String limit){

这时候直接访问时注意,要带完整的路径/swagger-ui/index.html。否则可能找不到,想要可以直接输入/swagger-ui就可以访问,需要配置拦截,对/swagger-ui路径,直接跳转到/swagger-ui/index.html。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值