侧边栏壁纸
  • 累计撰写 27 篇文章
  • 累计创建 9 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

解决swagger-ui中 空指针异常

仓鼠
2024-03-04 / 0 评论 / 0 点赞 / 26 阅读 / 3997 字 / 正在检测是否收录...

解决@ApiOperation 同一控制器导致swagger不可用问题

写代码偷懒,controller里直接复制一个接口,改把改把。@ApiOperation里的内容忘记修改了程序运行也不会报错。但是访问swagger-ui时出现了空指针异常。

以下为解决方案,添加一个友好提示


import io.swagger.annotations.ApiOperation;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
 * swagger-ui 校验是否存在相同内容 ApiOperation
 * 防止swagger-ui报错
 *
 * @author: yzy
 * @create: 2023/4/19
 * @Version 1.0
 **/
@Component
@Profile("dev")
public class SwaggerVerify implements ApplicationRunner {

    private final Logger log = Logger.getLogger("swagger 校验");

    private final List<RequestMappingHandlerMapping> handlerMappings;

    public SwaggerVerify(List<RequestMappingHandlerMapping> handlerMappings) {
        this.handlerMappings = handlerMappings;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Set<String> apiOperationValues = new HashSet<>();
        StringBuffer errorMsg = new StringBuffer();
        if(CollectionUtils.isEmpty(handlerMappings)){
            return;
        }
        try {
            for (RequestMappingHandlerMapping handlerMapping : handlerMappings) {
                if(handlerMapping == null || CollectionUtils.isEmpty(handlerMapping.getHandlerMethods())
                        || CollectionUtils.isEmpty(handlerMapping.getHandlerMethods().values())){
                    continue;
                }
                for (HandlerMethod handlerMethod : handlerMapping.getHandlerMethods().values()) {
                    String controller = handlerMethod.getBean().toString();
                    ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
                    if (apiOperation != null) {
                        if (!apiOperationValues.add(controller + apiOperation.value())) {
                            String message = String.format("\nERROR⚠⚠⚠\t存在相同的命名:[%s],异常位置:%s,异常bean:%s", apiOperation.value(), handlerMethod.toString(), controller);
                            errorMsg.append(message);
                        }
                    }
                }
            }
        }catch (Exception e){
            log.warning("swagger-校验模块出现异常!" + e.getMessage());
        }

        // 检查是否有相同的 value 值
        if (errorMsg.length() > 0) {
            log.warning(errorMsg.toString());
            throw new Exception("存在 @ApiOperation value 相同注解!" + errorMsg.toString());
        }
    }
}
0

评论区