SSM中使用网易易盾短信验证码服务时,遇到异常NoClassDefFoundError: Could not initialize class org.apache.http.client.fluent.Executor
控制器代码:
@PostMapping("/sendCode")
public ReturnMsg sendCode(@RequestParam("phone") String phone) {
//发送短信验证码
SendResponse sendResponse = SendSMSUtils.sendOtp(phone);
//将requestId存储保存到全局变量
requestId = sendResponse.getData().getRequestId();
return ReturnMsg.success("验证码发送成功");
}
发送验证码的方法,使用的易盾提供的示例代码
public static SendResponse sendOtp(String phone) {
// 这是你的 国内验证码短信 业务的 ID。可以登录易盾官网查看此业务 ID。
String businessId = "7736a8f77362442d87527b32cffd2672";
// 这是你事先创建好的模板,且已通过审核。
String templateId = "16309";
// 此处假设目标模板内容里只有验证码一个变量,所以没有其它变量需要指定
Map<String, String> variables = Collections.emptyMap();
// 发国内短信时,不指定 Country Calling Code
Map<String, String> param = createSendParam(businessId, templateId, variables, phone);
return RequestUtils.postForEntity(URI_SEND_SMS, param, SendResponse.class);
}
发送post请求
public static <R> R postForEntity(String uri, Map<String, String> params, Class<R> responseType) {
try {
String strResponse = Request.Post(uri)
.bodyForm(convertToFormData(params), StandardCharsets.UTF_8)
.execute()
.returnContent()
.asString();
return parseResponse(strResponse, responseType);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
执行execute()方法时抛出异常,NoClassDefFoundError: Could not initialize class org.apache.http.client.fluent.Executor
public Response execute() throws ClientProtocolException, IOException {
return new Response(this.internalExecute(Executor.CLIENT, (HttpContext)null));
}
Executor类
public class Executor {
static final PoolingHttpClientConnectionManager CONNMGR;
static final HttpClient CLIENT;
//...
static {
SSLConnectionSocketFactory ssl = null;
try {
ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
} catch (SSLInitializationException var7) {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init((KeyManager[])null, (TrustManager[])null, (SecureRandom)null);
ssl = new SSLConnectionSocketFactory(sslcontext);
} catch (SecurityException var4) {
} catch (KeyManagementException var5) {
} catch (NoSuchAlgorithmException var6) {
}
}
Registry<ConnectionSocketFactory> sfr = RegistryBuilder.create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory()).build();
CONNMGR = new PoolingHttpClientConnectionManager(sfr);
CONNMGR.setDefaultMaxPerRoute(100);
CONNMGR.setMaxTotal(200);
CONNMGR.setValidateAfterInactivity(1000);
CLIENT = HttpClientBuilder.create().setConnectionManager(CONNMGR).build();
}
整个示例代码在一个单独的项目里跑没有问题,但在我的ssm项目里就会抛下面的异常,求指点
运行结果及报错内容
Failed to complete request: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.apache.http.impl.conn.PoolingHttpClientConnectionManager.setValidateAfterInactivity(I)V
Failed to complete request: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.client.fluent.Executor