【问题标题】:Using RestTemplate on DesktopApplication (Non-WebBased)在桌面应用程序上使用 RestTemplate(非基于 Web)
【发布时间】:2020-08-11 16:01:02
【问题描述】:

我正在使用OpenJfxSpringBoot 开发桌面应用程序。到目前为止效果很好。

但是,此应用程序将使用 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


    【解决方案1】:

    RestTemplate 只是 HttpClient 实现的包装器。该 http 客户端需要由某人提供,例如底层服务器实现,如 tomcat、undertow、netty、jetty 等。但也可以来自独立客户端。

    因此,如果我们检查您的错误:

    [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] 
    

    这表示它无法启动服务器,因为您已拉入spring-web,这是很多需要服务器的类。它尝试autostart 很多类。

    spring.main.web-application-type=none
    

    因此,使用此参数,您基本上可以禁用所有 autostart 类,如果您有一个 Web 应用程序,这些类将运行。这不会禁用 spring 上下文的创建(需要上下文来创建所有 spring 类,以确保自动装配、bean 等工作)

    [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] 
    

    因此,当您现在运行它时,它会尝试创建上下文并失败,因为它找不到 Jackson,因为需要 jackson,并且是使用 您使用之前的参数禁用的 autostart 类创建的。

    它基本上是说您想使用汽车,但您不想要引擎、车轮、内饰、车门、方向盘等。如果您没有服务器或任何相关的东西来提供HttpClient 没有任何东西可供RestTemplate 使用或环绕。

    如果您正在寻找一个独立的简单休息客户端,我建议您查看OkHttp

    【讨论】:

      猜你喜欢
      • 2020-06-30
      • 2010-09-30
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多