java.lang.NoClassDefFoundError:org/apache/http/ssl/TrustStrategy,尽管它存在于类路径中

java.lang.NoClassDefFoundError:org/apache/http/ssl/TrustStrategy,尽管它存在于类路径中,java,apache,maven,ssl,jar,Java,Apache,Maven,Ssl,Jar,我正在tomcat服务器上运行一个应用程序。调用特定函数时出现以下错误: java.lang.NoClassDefFoundError:org/apache/http/ssl/TrustStrategy 我的班级是: import java.io.ByteArrayOutputStream; import java.io.Serializable; import java.util.Map; import javax.net.ssl.SSLContext; import org.apache

我正在tomcat服务器上运行一个应用程序。调用特定函数时出现以下错误:

java.lang.NoClassDefFoundError:org/apache/http/ssl/TrustStrategy

我的班级是:

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
/*import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;*/
//import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.performics.air.business.common.data.HttpParam;

public class SendAndReceiveUtil implements Serializable {
//FIXME: move to suitable package
    /**
     * 
     */
    private static final long serialVersionUID = 2649891233958197253L;
    private static Logger LOG = Logger.getLogger(SendAndReceiveUtil.class);


    public static String httpPostWithTLS(String request,String url,Map<String,String> headerParameterMap){
        String responseStr = null;
        try{
            // FIXME: need to handle supplier timeout and gzip

            String contentType="";
            String soapAction="";               
            boolean zipForRequest=false;
            boolean acceptEncoding = false; 
            boolean zipForResponse=false;
            if (headerParameterMap!=null){
                contentType=headerParameterMap.get(HttpParam.CONTENTTYPE.toString())!=null?headerParameterMap.get(HttpParam.CONTENTTYPE.toString()):"";
                zipForRequest=(headerParameterMap.containsKey(HttpParam.ZIPFORREQUEST.toString()))? new Boolean(headerParameterMap.get(HttpParam.ZIPFORREQUEST.toString())):false;
                acceptEncoding=(headerParameterMap.containsKey(HttpParam.ACCEPT_ENCODING.toString()))? new Boolean(headerParameterMap.get(HttpParam.ACCEPT_ENCODING.toString())):false;
                zipForResponse=(headerParameterMap.containsKey(HttpParam.ZIPFORRESPONSE.toString()))? new Boolean(headerParameterMap.get(HttpParam.ZIPFORRESPONSE.toString())):false;
                soapAction=headerParameterMap.get(HttpParam.SOAPACTION.toString())!=null?headerParameterMap.get(HttpParam.SOAPACTION.toString()):"";
            }

            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null,new TrustSelfSignedStrategy()).build();
            // Allow TLSv1.2 protocol only, use NoopHostnameVerifier to trust self-singed cert
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1.2" }, null, new NoopHostnameVerifier());
            //do not set connection manager
            HttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("SOAPAction", soapAction);

            StringEntity mEntity = new StringEntity(request, "UTF-8");

            if(StringUtils.isNotBlank(contentType)){
                mEntity.setContentType(contentType);
                mEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,contentType));
            }else{
                mEntity.setContentType("text/xml;charset=UTF-8");
                mEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING,"gzip"));
            }
            httpPost.addHeader("Content-Encoding", "gzip" );
            httpPost.addHeader("Accept-Encoding", "gzip,deflate" );
            if(null!=headerParameterMap.get("Cookie")){
                httpPost.addHeader("Cookie", headerParameterMap.get("Cookie"));
            }
            httpPost.setEntity(mEntity);
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity et=response.getEntity();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            et.writeTo(os);

            responseStr = new String(os.toByteArray());
            }catch(Exception e){
                LOG.error(e.getMessage(), e);
            }
        return responseStr;
    } 
}
import java.io.ByteArrayOutputStream;
导入java.io.Serializable;
导入java.util.Map;
导入javax.net.ssl.SSLContext;
导入org.apache.commons.lang.StringUtils;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpHost;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.config.RequestConfig;
导入org.apache.http.client.methods.HttpPost;
/*导入org.apache.http.client.config.AuthSchemes;
导入org.apache.http.client.config.CookieSpecs;
导入org.apache.http.client.config.RequestConfig*/
//导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.conn.ssl.NoopHostnameVerifier;
导入org.apache.http.conn.ssl.SSLConnectionSocketFactory;
导入org.apache.http.conn.ssl.SSLContexts;
导入org.apache.http.conn.ssl.TrustSelfSignedStrategy;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.HttpClients;
导入org.apache.http.message.BasicHeader;
导入org.apache.http.protocol.http;
导入org.apache.log4j.Logger;
导入org.performics.air.business.common.data.HttpParam;
公共类SendAndReceiveUtil实现可序列化{
//修正:移动到合适的包装
/**
* 
*/
私有静态最终长serialVersionUID=264989191233958197253L;
私有静态记录器LOG=Logger.getLogger(sendandereceiveutil.class);
公共静态字符串httpPostWithTLS(字符串请求、字符串url、映射头参数映射){
字符串responsest=null;
试一试{
//FIXME:需要处理供应商超时和gzip
字符串contentType=“”;
字符串soapAction=“”;
布尔zipForRequest=false;
布尔接受编码=假;
布尔zipForResponse=false;
if(headerParameterMap!=null){
contentType=headerParameterMap.get(HttpParam.contentType.toString())!=null?headerParameterMap.get(HttpParam.contentType.toString()):“”;
zipForRequest=(headerParameterMap.containsKey(HttpParam.zipForRequest.toString())?新布尔值(headerParameterMap.get(HttpParam.zipForRequest.toString()):false;
acceptEncoding=(headerParameterMap.containsKey(HttpParam.ACCEPT_ENCODING.toString())?新布尔值(headerParameterMap.get(HttpParam.ACCEPT_ENCODING.toString()):false;
zipForResponse=(headerParameterMap.containsKey(HttpParam.zipForResponse.toString())?新布尔值(headerParameterMap.get(HttpParam.zipForResponse.toString()):false;
soapAction=headerParameterMap.get(HttpParam.soapAction.toString())!=null?headerParameterMap.get(HttpParam.soapAction.toString()):“”;
}
SSLContext SSLContext=SSLContexts.custom().loadTrustMaterial(null,新的TrustSelfSignedStrategy()).build();
//仅允许TLSv1.2协议,使用NoopHostnameVerifier信任自签名证书
SSLConnectionSocketFactory sslsf=新SSLConnectionSocketFactory(sslcontext,新字符串[]{“TLSv1.2”},null,新NoopHostnameVerifier());
//不要设置连接管理器
HttpClient HttpClient=HttpClients.custom().setsslssocketfactory(sslsf.build();
HttpPost HttpPost=新的HttpPost(url);
setHeader(“SOAPAction”,SOAPAction);
StringEntity mEntity=新的StringEntity(请求,“UTF-8”);
if(StringUtils.isNotBlank(contentType)){
mEntity.setContentType(contentType);
setContentEncoding(新的BasicHeader(HTTP.CONTENT_-TYPE,contentType));
}否则{
setContentType(“text/xml;charset=UTF-8”);
setContentEncoding(新的BasicHeader(HTTP.CONTENT_编码,“gzip”);
}
addHeader(“内容编码”,“gzip”);
addHeader(“接受编码”、“gzip、deflate”);
if(null!=headerParameterMap.get(“Cookie”)){
httpPost.addHeader(“Cookie”,headerParameterMap.get(“Cookie”);
}
httpPost.setEntity(mEntity);
HttpResponse response=httpclient.execute(httpPost);
HttpEntity et=response.getEntity();
ByteArrayOutputStream os=新建ByteArrayOutputStream();
et.writeTo(os);
responsest=新字符串(os.toByteArray());
}捕获(例外e){
LOG.error(e.getMessage(),e);
}
返回响应;
} 
}
调用
httpPostWithTLS()
函数时出错。我在网上搜索了一下,发现这个类在编译时可用,但在运行时不可用,但我无法更正它。 我正在使用以下http JAR:

  • commons-httpclient-3.1.jar
  • httpclient-4.5.3.jar
  • httpcore-4.4.6.jar
  • httpclient-cache-4.5.3.jar
  • httpclient-win-4.5.3.jar
  • httpmime-4.5.3.jar

尝试将httpcore-4.4.6.jar和httpclient-cache-4.5.3.jar添加到服务器库并检查

在添加这些jar文件之后,我确实遇到了类似的问题,我的问题得到了解决


谢谢,

您是否使用WAR存档部署到Tomcat?你能展示一下你的POM描述吗?