springboot整合swagger

发布时间:2023年12月18日

1.pom.xml

<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>

2.SwaggerConfig

@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);
                }
            }
        };
    }
}

3.Controller

@Api(value = "xxx",tags = "xxx")
@RestController
@RequestMapping("/xxx")
public class BizxxxController {
    @ApiOperation(value = "xxx")
    @PostMapping("/xxx")
    public ResultResponse<String> xxx(){
        return ResultResponse.succResult("success");
    }
}

4.请求参数

@Data
@ApiModel(value = "AddParam",description = "新增")
public class AddParam {
    @ApiModelProperty("xxx")
    private Long xxxx ;
}

5.swagger配置释义

@Api:用于描述接口的类或接口
@ApiOperation:用于描述接口的方法
@ApiParam:用于描述接口的参数
@ApiModel:用于描述数据模型
@ApiModelProperty:用于描述数据模型的属性

6.swagger访问地址

http://localhost:8080/xx/swagger-ui/index.html

7.注意事项

@Api(value = “xxx”,tags = “xxx”)
@ApiModel(value = “AddParam”,description = “新增”)
value、tag属性,value、description都需要标注,复制很容易在swagger接口页面访问时,出现不通接口请求参数一致的问题

文章来源:https://blog.csdn.net/Nice_EveryDay/article/details/134987348
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。