腾讯短信集成报错误:NoClassDefFoundError: org/apache/http/client/config/RequestConfig

本文描述了在Maven项目中使用腾讯云短信SDK遇到的NoClassDefFoundError错误,分析了问题原因是由于腾讯云SDK依赖的httpclient版本与项目中已有的httpclient版本冲突导致。提供了两种解决方案,一是调整项目中httpclient版本与SDK一致,二是使用maven-shade插件重命名冲突包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.问题描述

今天在使用腾讯短信sdk进行Maven项目依赖过程中
添加以下依赖包:

<!-- 腾讯云-短信功能 -->
		<dependency>
  			<groupId>com.github.qcloudsms</groupId>
  			<artifactId>qcloudsms</artifactId>
  			<version>1.0.6</version>
		</dependency>

调用短信发送方法(模板发送)时项目报错,如下:

rg.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/client/config/RequestConfig
at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1305)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:979)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
......
Caused by: java.lang.NoClassDefFoundError: org/apache/http/client/config/RequestConfig
at com.github.qcloudsms.httpclient.DefaultHTTPClient.fetch(DefaultHTTPClient.java:29)
at com.github.qcloudsms.SmsSingleSender.sendWithParam(SmsSingleSender.java:127)
at com.github.qcloudsms.SmsSingleSender.sendWithParam(SmsSingleSender.java:143)
at com.dz.sms.SmsUtil.sendTemplateSingleSms(SmsUtil.java:65)

二.原因分析

1.经过网上查找,大多数都说是缺少 httpclient
但本项目中已经添加了 httpclient 包:

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

2.查看 Maven 依赖关系
发现 腾讯 qcloudsms 包依赖使用的 httpclient 4.5.3 ,原因终于找到了
jar包直接有冲突!
在这里插入图片描述

三.解决方法

方法1.修改原项目中的 httpclient 包版本号和 qcloudsms 中的版本号一致(4.5.3)。
方法2.使用 maven-shade 插件,过滤包冲突

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <relocations>
              <relocation>
                <pattern>org.apache.httpcomponents</pattern>
                <shadedPattern>org.shaded.org.apache.httpcomponents</shadedPattern>
              </relocation>
            </relocations>
        </configuration>
    </execution>
</executions>
</plugin>

主要是配置使用 relocations 重命名冲突包

   <configuration>
            <relocations>
              <relocation>
                <pattern>org.apache.httpcomponents</pattern>
                <shadedPattern>org.shaded.org.apache.httpcomponents</shadedPattern>
              </relocation>
            </relocations>
        </configuration>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值