我尝试使用SendGrid sg = new SendGrid(sendGridAPIKEY);
时遇到异常
它给出了异常,因为发送网格在内部使用HttpClientBuilder.create().build();
。
我通过添加以下行解决了这个问题
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
Client sendGridClient = new Client(defaultHttpClient, false);
SendGrid sg = new SendGrid(sendGridAPIKEY, sendGridClient);
不推荐使用DefaultHttpClient
。
但我想知道为什么HttpClientBuilder.create().build()
行会出错。
我正在使用JDK 1.8并遵循POM依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<!-- send Grid dependency -->
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.0.1</version>
</dependency>
例外:
Nov 06, 2017 10:15:26 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V] with root cause
java.lang.NoSuchMethodError: org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:176)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:158)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:973)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at com.sendgrid.Client.<init>(Client.java:76)
at com.sendgrid.Client.<init>(Client.java:56)
at com.sendgrid.SendGrid.<init>(SendGrid.java:24)