[英]Why I can't retrieve this file when I run the .jar version of my Spring Boot application?
I am working on a Spring Batch application and I am finding some problem retrieving an SSL certificate file (used from RestTemplate ) when I execute the .jar file containing my application.我正在处理 Spring Batch 应用程序,当我执行包含我的应用程序的.jar文件时,我发现在检索 SSL 证书文件(从RestTemplate使用)时出现一些问题。 My application works fine if I run it from STS (Eclipse).
如果我从 STS (Eclipse) 运行我的应用程序,它可以正常工作。
I will try to explain in details what I have done and what is the problem.我将尝试详细解释我所做的事情以及问题所在。 Into this folder of my project /src/main/resources/static I have put my certificate file named certificate.pfx :
在我的项目/src/main/resources/static的这个文件夹中,我将我的证书文件命名为certificate.pfx :
Then into my code I declaring this method returning a RestTemplate bean that retrieve and use this certificate file in order to perform REST call to an external API:然后在我的代码中声明此方法返回一个RestTemplate bean,该 bean 检索并使用此证书文件以执行对外部 API 的 REST 调用:
@Bean
RestTemplate restTemplate() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, URISyntaxException {
final String password = "password";
//final String pfxPath = "/home/andrea/Documents/workspace-spring-tool-suite-4-4.11.0.RELEASE/UpdateInfoBatch/target/classes/static/certificate.pfx";
URL clientCertRes = getClass().getClassLoader().getResource("static/certificate.pfx");
File clientCertFileile = Paths.get(clientCertRes.toURI()).toFile();
String clientCertFileileAbsolutePath = clientCertFileile.getAbsolutePath();
System.out.println("clientCertFileileAbsolutePath: " + clientCertFileileAbsolutePath);
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = getHttpComponentsClientHttpRequestFactory(clientCertFileileAbsolutePath,password);
RestTemplate restTemplate = new RestTemplate(httpComponentsClientHttpRequestFactory);
return restTemplate;
}
If I execute my code from STS\Eclipse it works fine: I have no exception and the call to my external API correctly uses the needed certificate.如果我从 STS\Eclipse 执行我的代码,它工作正常:我没有例外,对我的外部 API 的调用正确使用了所需的证书。
The problem is that when I try to perform my application executing the compiled .jar file version.问题是,当我尝试执行我的应用程序时,会执行已编译的.jar文件版本。 Infact if I execute:
事实上,如果我执行:
java -jar UpdateInfoBatch-0.0.1-SNAPSHOT.jar
I am obtaining the following exception starting the application context:我在启动应用程序上下文时获得以下异常:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-24 19:04:37.115 ERROR 6544 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springBatchExampleJobLauncher' defined in URL [jar:file:/home/andrea/git/notartel-api-batch/target/UpdateInfoBatch-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/notariato/updateInfo/SpringBatchExampleJobLauncher.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'updateInfoBatchConfig': Unsatisfied dependency expressed through field 'notaryListServiceAdapter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'notaryListServiceAdapter': Unsatisfied dependency expressed through field 'notaryService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notaryService' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.notariato.service.NotaryService]: Factory method 'notaryService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.9.jar!/:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar!/:5.3.9]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.3.jar!/:2.5.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.3.jar!/:2.5.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.3.jar!/:2.5.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.3.jar!/:2.5.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.3.jar!/:2.5.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.3.jar!/:2.5.3]
at com.notariato.updateInfo.UpdateInfoBatchApplication.main(UpdateInfoBatchApplication.java:35) ~[classes!/:0.0.1-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[UpdateInfoBatch-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[UpdateInfoBatch-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[UpdateInfoBatch-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[UpdateInfoBatch-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'updateInfoBatchConfig': Unsatisfied dependency expressed through field 'notaryListServiceAdapter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'notaryListServiceAdapter': Unsatisfied dependency expressed through field 'notaryService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notaryService' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.notariato.service.NotaryService]: Factory method 'notaryService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:410) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 27 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'notaryListServiceAdapter': Unsatisfied dependency expressed through field 'notaryService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notaryService' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.notariato.service.NotaryService]: Factory method 'notaryService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 51 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notaryService' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.notariato.service.NotaryService]: Factory method 'notaryService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 65 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.notariato.service.NotaryService]: Factory method 'notaryService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 78 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/notariato/updateInfo/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:362) ~[spring-context-5.3.9.jar!/:5.3.9]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:334) ~[spring-context-5.3.9.jar!/:5.3.9]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284.restTemplate(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at com.notariato.updateInfo.config.BeanConfig.notaryService(BeanConfig.java:34) ~[classes!/:0.0.1-SNAPSHOT]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284.CGLIB$notaryService$1(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284$$FastClassBySpringCGLIB$$3144f839.invoke(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.9.jar!/:5.3.9]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.9.jar!/:5.3.9]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284.notaryService(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 79 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.nio.file.FileSystemNotFoundException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.9.jar!/:5.3.9]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 102 common frames omitted
Caused by: java.nio.file.FileSystemNotFoundException: null
at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:156) ~[jdk.zipfs:na]
at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:142) ~[jdk.zipfs:na]
at java.base/java.nio.file.Path.of(Path.java:208) ~[na:na]
at java.base/java.nio.file.Paths.get(Paths.java:98) ~[na:na]
at com.notariato.updateInfo.config.BeanConfig.restTemplate(BeanConfig.java:47) ~[classes!/:0.0.1-SNAPSHOT]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284.CGLIB$restTemplate$0(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284$$FastClassBySpringCGLIB$$3144f839.invoke(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.9.jar!/:5.3.9]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.9.jar!/:5.3.9]
at com.notariato.updateInfo.config.BeanConfig$$EnhancerBySpringCGLIB$$e4e5b284.restTemplate(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.9.jar!/:5.3.9]
... 103 common frames omitted
As you can see in the previous stacktrace the cause of this exception is this one:正如您在前面的堆栈跟踪中看到的,此异常的原因是:
Caused by: java.nio.file.FileSystemNotFoundException: null
and I am pretty sure that it should be related to these lines of code where I retrieve the certificate file:而且我很确定它应该与我检索证书文件的这些代码行相关:
URL clientCertRes = getClass().getClassLoader().getResource("static/certificate.pfx");
File clientCertFileile = Paths.get(clientCertRes.toURI()).toFile();
String clientCertFileileAbsolutePath = clientCertFileile.getAbsolutePath();
I am pretty sure because I tried to delete all the lines related to obtain certificate file and I simply returned a classic RestTemplate object:我很确定,因为我试图删除所有与获取证书文件相关的行,我只是返回了一个经典的 RestTemplate object:
RestTemplate restTemplate = new RestTemplate();
doing in this way I obtain no more the previous exception (but instead I obtain the expected SSL exception because to perform my call I need this certificate).这样做我不再获得以前的异常(而是我获得了预期的 SSL 异常,因为要执行我的调用我需要这个证书)。
So...what is wrong?那么……怎么了? what am I missing?
我错过了什么? How can I try to fix this issue?
我该如何尝试解决这个问题?
What could be a good solution?什么是好的解决方案?
To embed the certificate directly into the .jar file?要将证书直接嵌入到.jar文件中? (in this case I will have an all in one solution)
(在这种情况下,我将拥有一个一体化的解决方案)
To point to a specific directory of my file system where I will put my certificate?要指向我的文件系统的特定目录,我将在其中放置我的证书? (maybe it could be better because if in the future the certificate will change I can simply replace it into the file system avoiding to recompile and redeploy my application)
(也许它会更好,因为如果将来证书会改变,我可以简单地将它替换到文件系统中,避免重新编译和重新部署我的应用程序)
What do you think about?你有什么想法?
Code in this answer is a guess because I do not know what is inside the getHttpComponentsClientHttpRequestFactory
:).这个答案中的代码是一个猜测,因为我不知道
getHttpComponentsClientHttpRequestFactory
里面有什么:)。 But I think, you can solve the problem as follows:但我认为,您可以按以下方式解决问题:
File
to URL
File
更改为URL
ClassPathResource
to get the certificate's URL
ClassPathResource
获取证书的URL
URL
URL
I guess you're using custom SSLContext
created using the SSLContexts.custom()
returned builder, something similar to:我猜您正在使用使用
SSLContexts.custom()
返回的构建器创建的自定义SSLContext
,类似于:
SSLContexts.custom()
.loadTrustMaterial(new File("/tmp/fred.zip"), "apples".toCharArray())
.build();
You can use the overload accepting URL
instead:您可以改用接受
URL
的重载:
SSLContexts.custom()
.loadTrustMaterial(new URL("...."), "apples".toCharArray())
.build();
ClassPathResource
to obtain URL
ClassPathResource
获取URL
For example:例如:
ClassPathResource resource = new ClassPathResource("/fred.pfx", SpringBootResourcesApplication.class);
URL url = resource.getURL();
fred.pfx
is the file I'm loading. fred.pfx
是我正在加载的文件。 It is placed directly in resources
directoryresources
目录下SpringBootResourcesApplication.class
is the class that loads the file SpringBootResourcesApplication.class
就是加载文件的class This will work in IDE and in jar file这将在 IDE 和 jar 文件中工作
public class SpringBootResourcesApplication {
public static void main(String[] args) throws Exception {
ClassPathResource resource = new ClassPathResource("/fred.pfx", SpringBootResourcesApplication.class);
URL url = resource.getURL();
System.out.println(url);
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(url, "apples".toCharArray())
.build();
System.out.println(sslContext);
}
}
file:/home/caco3/IdeaProjects/spring-boot-resources/target/classes/fred.pfx javax.net.ssl.SSLContext@217ed35e
jar:file:/home/caco3/IdeaProjects/spring-boot-resources/target/spring-boot-resources-0.0.1-SNAPSHOT.jar./BOOT-INF/classes./fred.pfx javax.net.ssl.SSLContext@3444d69d
java.nio.file.FileSystemNotFoundException
thrownjava.nio.file.FileSystemNotFoundException
Path.get
does not always succeed. Path.get
并不总是成功。 When you get a URL
for the Path
using code similar to:当您使用类似于以下代码的
Path
获得URL
时:
URL resource = SpringBootResourcesApplication.class.getClassLoader().getResource("application.properties");
The result can be different, for example:结果可能不同,例如:
file:/home/caco3/IdeaProjects/spring-boot-resources/target/classes/application.properties
jar:file:/home/caco3/IdeaProjects/spring-boot-resources/target/spring-boot-resources-0.0.1-SNAPSHOT.jar./BOOT-INF/classes!/application.properties
These URI
s are handled differently:这些
URI
的处理方式不同:
file:/...
URI
can be successfully converted to a Path
file:/...
URI
可以成功转换为Path
jar:file:/...
cannot: it requires an explicit FileSystem
to be createdjar:file:/...
不能:它需要创建一个明确的FileSystem
系统That's why the exception was thrown这就是抛出异常的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.