springboot 2.7.17 整合 swagger3

gradle

io.springfox:springfox-boot-starter:3.0.0

yaml

spring:
  mvc: 
    pathmatch:
      matching-strategy: ant_path_matcher

SwaggerConfig

package com.hz.auth.config;


import io.swagger.annotations.Api;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                // RequestHandlerSelectors.any()   为任何接口生成API文档
                // RequestHandlerSelectors.none()  不生成任何接口文档
                // RequestHandlerSelectors.basePackage("com.neutech.swagger2study")  只为这个包下生成接口文档
                // RequestHandlerSelectors.withClassAnnotation(Api.class)  为有@Api注解的Controller生成API文档
                // RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class) 为有@ApiOperation注解的方法生成API文档
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                // PathSelectors.ant("/api/**") 过滤路径 只扫描/api/**的接口 生成接口文档
                // .paths(PathSelectors.ant("/api/**"))
                .build();
    }

    // swagger信息
    private ApiInfo apiInfo() {
        // 作者信息
        Contact contact = new Contact("sc", "", "xxx@163.com");
        return new ApiInfo("智能终端----登录服务API接口文档",
                "描述",
                "v1.0",
                "http://localhost:8080/swagger-ui.html",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

    @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) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }
}

controller代码

package com.hz.meeting.controller;

import com.hz.common.entity.res.Response;
import com.hz.meeting.entity.bo.CallTreeBo;
import com.hz.meeting.entity.vo.DevicesOrPersonVo;
import com.hz.meeting.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@Slf4j
@RequestMapping("/common")
@Api(tags = "公共服务接口")
public class CommonController {
    @Resource
    private CommonService commonService;

    @ApiOperation("xxxxxxxxx")
    @GetMapping("getBindAddressName/{cloudRoomMeetingUserId}")
    public Response<CallTreeBo> getBindAddressNameByCloudRoomMeetingUserId(@PathVariable String cloudRoomMeetingUserId) {
        return Response.success(commonService.getCallTreeByCloudRoomUserId(cloudRoomMeetingUserId));
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值