简体   繁体   English

创建自定义RestTemplate时,Spring-boot会出错

[英]Spring-boot gets an error when creating custom RestTemplate

I have a sendGetREST method to send some URL endpoint and get the response: 我有一个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;
    }

  }
}

Which is working well. 哪个运作良好。 However, in order to make the timeout time customizable, I have this HTTPConfiguration from this tutorial : 但是,为了使超时时间可以自定义,我从本教程中获得了这个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;
   }
}

So the class will be calling restTemplate like this: 所以该类将调用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;
    }

  }
}

But unfortunately return this following error when builds the spring-boot app: 但不幸的是,在构建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

FYI the error is much more than displayed above. 仅供参考,错误远远超过上面显示的错误。

The first line is something like: 第一行是这样的:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource .....
Caused by: java.lang.NoClassDefFoundError: org/apache/http/protocol/HttpContext

The error shows that there was a problem with your classpath, it could be because there is no class or.apache.http.protocol.HttpContext in your classpath. 该错误表明您的类路径存在问题,可能是因为类路径中没有类or.apache.http.protocol.HttpContext。 This class is often used by Spring RestTemplate. Spring RestTemplate经常使用这个类。 Normally we can find this class in the Apache httpclient library. 通常我们可以在Apache httpclient库中找到这个类。 You should check again your dependencies. 您应该再次检查您的依赖项。 If you can not find it, you can consider to add below dependency (if you use Maven) 如果找不到,可以考虑添加以下依赖项(如果使用Maven)

 <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM