Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在DesktopApplication上使用RestTemplate(非基于Web)_Java_Spring_Spring Boot_Spring Mvc_Resttemplate - Fatal编程技术网

Java 在DesktopApplication上使用RestTemplate(非基于Web)

Java 在DesktopApplication上使用RestTemplate(非基于Web),java,spring,spring-boot,spring-mvc,resttemplate,Java,Spring,Spring Boot,Spring Mvc,Resttemplate,我正在使用OpenJfx和SpringBoot开发一个桌面应用程序。到目前为止,它运作良好 但是,这个应用程序将使用一个Web服务,我想使用RestTemplate。当我将springweb依赖项添加到pom.xml文件时,springboot会尝试实例化一些我没有用过的东西 依赖项添加了: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-

我正在使用
OpenJfx
SpringBoot
开发一个桌面应用程序。到目前为止,它运作良好

但是,这个应用程序将使用一个Web服务,我想使用RestTemplate。当我将
springweb
依赖项添加到pom.xml文件时,springboot会尝试实例化一些我没有用过的东西

依赖项添加了:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.7.RELEASE</version>
</dependency>
根据回复,我添加了以下属性:

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服务。有没有一种方法可以将类添加到其他spring库中,而无需承担所有额外的负担?

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] 
这意味着它无法启动服务器,因为您已经拉入了
springweb
,这是许多需要服务器的类。它尝试
autostart
许多类

spring.main.web-application-type=none
因此,使用此参数,基本上可以禁用所有将在web应用程序中运行的
autostart
类。这不会禁用spring上下文的创建(创建所有spring类都需要该上下文,以确保自动连接工作、bean等)

[2020-08-11 10:29:47]-警告-上下文初始化过程中遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatifiedpendencyException:创建名为“jacksonObjectMapper”的bean时出错,该bean在类路径资源中定义[org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperConfiguration.class]:通过方法“jacksonObjectMapper”参数0表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名为“jacksonObjectMapperBuilder”的bean时出错[org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration.class]:通过工厂方法实例化Bean失败;嵌套异常为org.springframework.beans.beanstantiationexception:实例化[org.springframework.http.converter.json.Jackson2ObjectMapperBuilder]失败:工厂方法“jacksonObjectMapperBuilder”引发异常;嵌套异常为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
就没有什么可使用或包装的


如果您正在寻找一个独立的简单rest客户端,我建议您研究一下

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] 
这意味着它无法启动服务器,因为您已经拉入了
springweb
,这是许多需要服务器的类。它尝试
autostart
许多类

spring.main.web-application-type=none
因此,使用此参数,基本上可以禁用所有将在web应用程序中运行的
autostart
类。这不会禁用spring上下文的创建(创建所有spring类需要上下文,以确保自动连接工作,Bean等)

[2020-08-11 10:29:47]-警告-上下文初始化过程中遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatifiedpendencyException:创建名为“jacksonObjectMapper”的bean时出错,该bean在类路径资源中定义[org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperConfiguration.class]:通过方法“jacksonObjectMapper”参数0表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名为“jacksonObjectMapperBuilder”的bean时出错[org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration.class]:通过工厂方法实例化Bean失败;嵌套异常为org.springframework.beans.beanstantiationexception:实例化[org.springframework.http.converter.json.Jackson2ObjectMapperBuilder]失败:工厂方法“jacksonObjectMapperBuilder”引发异常;嵌套异常为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
类创建的