使用knife4j-openapi3-spring-boot-starter,打开doc.html页面报stackoverflow异常

2023-10-16 19:58:05 浏览数 (4)

        knife4j是一款不错集swagger2以及openapi3的增强方案,具体可以查看官网:

        本文提及的这个问题在swagger2正常,但是迁移到openapi3上时却出现了stackoverflow异常,部分异常信息如下所示:

Caused by: java.lang.StackOverflowError: null at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:423) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.calculateHashCode(ResolvableType.java:938) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.(ResolvableType.java:148) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.forType(ResolvableType.java:1436) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.forType(ResolvableType.java:1405) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.getGenerics(ResolvableType.java:720) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.getGenerics(ResolvableType.java:724) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.ResolvableType.hasUnresolvableGenerics(ResolvableType.java:548) ~[spring-core-5.3.23.jar:5.3.23] at org.springdoc.core.ReturnTypeParser.resolveType(ReturnTypeParser.java:87) ~[springdoc-openapi-common-1.6.15.jar:1.6.15] at org.springdoc.core.ReturnTypeParser.resolveType(ReturnTypeParser.java:93) ~[springdoc-openapi-common-1.6.15.jar:1.6.15] at org.springdoc.core.ReturnTypeParser.resolveType(ReturnTypeParser.java:93) ~[springdoc-openapi-common-1.6.15.jar:1.6.1

从上面标红部分可以断定是泛型类的类型确定不了,场景如下所示:

代码语言:javascript复制
// 出问题的类
public class SysOrgs<T extends SysOrgs<T>> {

    private String name;
    
    private List<T> children;
}

// controller某接口

@GetMapping
public List<SysOrgs> getOrgs(....){
}

问题就出在上面的controller使用了一个没指定泛型类型的SysOrgs基础类,导致openapi3在生成对应接口文档时无法解析的情况而导致了死循环,最终报了stackoverflow异常

1 人点赞