Spring-boot 在创建自定义 RestTemplate 时出现错误
2022-09-03 12:30:53
我有一种方法可以发送一些并获得响应:sendGetREST
URL endpoint
@Component
public class HttpURLCommand {
private static Logger logger = Logger.getLogger(HttpURLCommand.class);
public String sendGetREST(String soapUrl, Object[] parameters) throws IOException{
final String SOAP_URL = MessageFormat.format(soapUrl, parameters);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(SOAP_URL, String.class);
logger.info("status code is: "+response.getStatusCode());
if(response.getStatusCode().equals(HttpStatus.OK)){
logger.info("send success");
return response.getBody();
}
else {
logger.info("send failed");
return null;
}
}
}
这很好用。但是,为了使超时时间可自定义,我从本教程中得到了这个:HTTPConfiguration
@Configuration
public class HTTPConfiguration {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(60000);
factory.setConnectTimeout(60000);
return factory;
}
}
因此,该类将像这样调用 restTemplate:
@Component
public class HttpURLCommand {
private static Logger logger = Logger.getLogger(HttpURLCommand.class);
@Autowired
RestTemplate restTemplate;
public String sendGetREST(String soapUrl, Object[] parameters) throws IOException{
final String SOAP_URL = MessageFormat.format(soapUrl, parameters);
ResponseEntity<String> response = restTemplate.getForEntity(SOAP_URL, String.class);
logger.info("status code is: "+response.getStatusCode());
if(response.getStatusCode().equals(HttpStatus.OK)){
logger.info("send success");
return response.getBody();
}
else {
logger.info("send failed");
return null;
}
}
}
但不幸的是,在构建应用程序时返回以下错误:spring-boot
Caused by: java.lang.NoClassDefFoundError: org/apache/http/protocol/HttpContext
at com.xl.MbbI.config.HTTPConfiguration.clientHttpRequestFactory(HTTPConfiguration.java:25) ~[classes/:na]
at com.xl.MbbI.config.HTTPConfiguration.restTemplate(HTTPConfiguration.java:21) ~[classes/:na]
at com.xl.MbbI.config.HTTPConfiguration$$EnhancerBySpringCGLIB$$191f99f.CGLIB$restTemplate$0(<generated>) ~[na:na]
at com.xl.MbbI.config.HTTPConfiguration$$EnhancerBySpringCGLIB$$191f99f$$FastClassBySpringCGLIB$$8330ce57.invoke(<generated>) ~[na:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.3.0.RC2.jar:4.3.0.RC2]
at com.xl.MbbI.config.HTTPConfiguration$$EnhancerBySpringCGLIB$$191f99f.restTemplate(<generated>) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_60]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_60]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
... 24 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.http.protocol.HttpContext
at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.7.0_60]
at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.7.0_60]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_60]
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.7.0_60]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.7.0_60]
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.7.0_60]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.7.0_60]
... 36 common frames omitted
仅供参考,错误比上面显示的要多得多。
第一行是这样的:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource .....