【发布时间】:2020-08-11 16:01:02
【问题描述】:
我正在使用OpenJfx 和SpringBoot 开发桌面应用程序。到目前为止效果很好。
但是,此应用程序将使用 WebService,我想使用 RestTemplate。当我将 spring-web 依赖项添加到我的 pom.xml 文件时,Spring Boot 会尝试实例化一些我没有使用的东西。
添加的依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
首先显示如下错误:
[2020-08-11 10:26:20] - WARN - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. [AbstractApplicationContext.java:558]
根据this 回复,我添加了以下属性:
spring.main.web-application-type=none
再次运行应用程序,更多的东西尝试实例化:
[2020-08-11 10:29:47] - WARN - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jacksonObjectMapper' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperConfiguration.class]: Unsatisfied dependency expressed through method 'jacksonObjectMapper' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonObjectMapperBuilder' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.http.converter.json.Jackson2ObjectMapperBuilder]: Factory method 'jacksonObjectMapperBuilder' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.visibility(Lcom/fasterxml/jackson/annotation/PropertyAccessor;Lcom/fasterxml/jackson/annotation/JsonAutoDetect$Visibility;)Lorg/springframework/http/converter/json/Jackson2ObjectMapperBuilder; [AbstractApplicationContext.java:558]
[2020-08-11 10:29:47] - INFO -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. [ConditionEvaluationReportLoggingListener.java:136]
[2020-08-11 10:29:47] - ERROR -
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration$StandardJackson2ObjectMapperBuilderCustomizer.configureVisibility(JacksonAutoConfiguration.java:258)
The following method did not exist:
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.visibility(Lcom/fasterxml/jackson/annotation/PropertyAccessor;Lcom/fasterxml/jackson/annotation/JsonAutoDetect$Visibility;)Lorg/springframework/http/converter/json/Jackson2ObjectMapperBuilder;
The method's class, org.springframework.http.converter.json.Jackson2ObjectMapperBuilder, is available from the following locations:
jar:file:/C:/Users/xxx/.m2/repository/org/springframework/spring-web/5.0.7.RELEASE/spring-web-5.0.7.RELEASE.jar!/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.class
It was loaded from the following location:
file:/C:/Users/xxx/.m2/repository/org/springframework/spring-web/5.0.7.RELEASE/spring-web-5.0.7.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
[LoggingFailureAnalysisReporter.java:40]
问题是,我只想使用 RestTemplate 来消费一个 Rest Service。有没有一种方法可以将类本地添加到其他 Spring 库,而无需所有额外的包袱?
【问题讨论】:
标签: java spring spring-boot spring-mvc resttemplate