<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@Configuration
@Slf4j
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).groupName("de-an")
.select()
.apis(RequestHandlerSelectors.basePackage("com.de.an.datacenter"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口管理1.0.0." + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS"))
.format(Calendar.getInstance().getTime()))
.description("接口管理1.0.0.0")
.version("1.0.0-SNAPSHOT")
.build();
}
/**
* 解决swagger2.x版本与springboot2.x版本冲突问题
* @return
*/
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
log.warn("修改WebMvcRequestHandlerProvider的属性:handlerMappings出错,可能导致swagger不可用", e);
throw new IllegalStateException(e);
}
}
};
}
}
@Api(value = "xxx",tags = "xxx")
@RestController
@RequestMapping("/xxx")
public class BizxxxController {
@ApiOperation(value = "xxx")
@PostMapping("/xxx")
public ResultResponse<String> xxx(){
return ResultResponse.succResult("success");
}
}
@Data
@ApiModel(value = "AddParam",description = "新增")
public class AddParam {
@ApiModelProperty("xxx")
private Long xxxx ;
}
@Api:用于描述接口的类或接口
@ApiOperation:用于描述接口的方法
@ApiParam:用于描述接口的参数
@ApiModel:用于描述数据模型
@ApiModelProperty:用于描述数据模型的属性
http://localhost:8080/xx/swagger-ui/index.html
@Api(value = “xxx”,tags = “xxx”)
@ApiModel(value = “AddParam”,description = “新增”)
value、tag属性,value、description都需要标注,复制很容易在swagger接口页面访问时,出现不通接口请求参数一致的问题