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。