基于springboot 2.2.x & swagger 3.0
依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
// 是否开启
.enable(true)
.select()
.apis(RequestHandlerSelectors.basePackage("com.logic.transocean"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("transocean")
//创建人
.contact(new Contact("jack.zhou", "", "jacketzc@foxmail.conm"))
.version("1.0")
.description("")
.build();
}
}
常用注解
@Api
用于Controller,描述api信息
要用tags,默认的值好像不太管用
@Api(tags = "授权中心")
@RequestMapping("/oauth")
@RestController
public class OAuthController{}
@ApiOperation
用于Controller的method,描述api信息
@ApiOperation("获取授权")
@PostMapping("/authorize")
public Response authorize(@RequestBody OAuthRequestDTO oAuthRequestDTO) {
return ResponseUtil.returnSuccess(oauthService.authorize(oAuthRequestDTO));
}
@ApiModel
用于实体
@ApiModel(description = "oauth请求参数")
@Data
public class OAuthRequestDTO {}
@ApiModelProperty
用于实体的属性
@ApiModelProperty("客户端id")
private String client_id;
效果
更换第三方ui
这边就以knife4j为例,直接引入就好了,它已经包含了swagger的依赖,配置和上面一样的
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
常见问题与解决方法
doc.html 404
看看有些配置类是不是继承
WebMvcConfigurationSupport
的,应该改成实现WebMvcConfigurer
接口事实上,不建议继承
WebMvcConfigurationSupport
去定义配置,因为该操作会使得原有的自动配置失效,观察mvc自动配置的注解就可以知道:// 如果自定义了WebMvcConfigurationSupport的bean,则默认配置就不生效了
@ConditionalOnMissingBean(WebMvcConfigurationSuport.class)
public class WebMvcAutoConfiguration{}
springboot 2.6.x启动报错
添加以下配置(治标不治本):
spring.mvc.pathmatch.matching-strategy=ant_path_matcher