Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

链式调用时为未初始化值或无默认值导致NPE问题(如果配置类为做新版本兼容配置项) #223

Open
BingChunMoLi opened this issue Nov 4, 2021 · 2 comments

Comments

@BingChunMoLi
Copy link

public class SwaggerProperties{
    private List<SwaggerProperties.GlobalOperationParameter> globalOperationParameters;
}
public class DocketConfiguration implements BeanFactoryAware{
docket4Group.host(this.swaggerProperties.getHost()).apiInfo(apiInfo).globalRequestParameters(this.assemblyRequestParameters(this.swaggerProperties.getGlobalOperationParameters(), new ArrayList())).securityContexts(Collections.singletonList(this.authConfiguration.securityContext())).securitySchemes(this.authConfiguration.getSecuritySchemes()).select().apis(RequestHandlerSelectors.basePackage(this.swaggerProperties.getBasePackage())).paths(this.paths(this.swaggerProperties.getBasePath(), this.swaggerProperties.getExcludePath())).build();
}
//第74行 public void createSpringFoxRestApi(){}

如项目中未配置

swagger.globalOperationParameters[0].name=name one
swagger.globalOperationParameters[0].description=some description one

NPE
堆栈信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createSpringFoxRestApi' defined in class path resource [com/spring4all/swagger/DocketConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [void]: Factory method 'createSpringFoxRestApi' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656)
... 34 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [void]: Factory method 'createSpringFoxRestApi' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 49 common frames omitted
Caused by: java.lang.NullPointerException: null
at com.spring4all.swagger.DocketConfiguration.getRequestParameters(DocketConfiguration.java:153)
at com.spring4all.swagger.DocketConfiguration.assemblyRequestParameters(DocketConfiguration.java:175)
at com.spring4all.swagger.DocketConfiguration.createSpringFoxRestApi(DocketConfiguration.java:79)
at com.spring4all.swagger.DocketConfiguration$$EnhancerBySpringCGLIB$$25e37900.CGLIB$createSpringFoxRestApi$1()
at com.spring4all.swagger.DocketConfiguration$$EnhancerBySpringCGLIB$$25e37900$$FastClassBySpringCGLIB$$3cd76e23.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at com.spring4all.swagger.DocketConfiguration$$EnhancerBySpringCGLIB$$25e37900.createSpringFoxRestApi()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 50 common frames omitted

@MR03web
Copy link

MR03web commented Nov 4, 2021

2.5.6测试,至少需要配置一个globalOperationParameters

@BingChunMoLi
Copy link
Author

我希望是,如果简单项目或新手可以不配置该选项,至少能启动会比较好,给予默认值或者基本的初始化,所以我认为这是一个BUG

llin6025 added a commit to llin6025/spring-boot-starter-swagger that referenced this issue Nov 18, 2021
dyc87112 added a commit that referenced this issue Dec 26, 2021
fix: Can not configure globalOperationParameters #222 #223
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants