HttpCore HttpClient版本冲突引起的:java.lang.NoSuchFieldError:INSTANCE

时间:2016-01-19 09:33:57

标签: java spring-mvc httpclient hybris

我遇到HttpClient和HttpCore类的冲突问题。     当我尝试启动Tomcat服务器时,我在这个类上有这个例外:     NoSuchFieldError:实例。     这是我的代码:

package com.accenture.hybris.wsmonitoring.controllers;

import java.io.IOException;   
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.accenture.hybris.wsmonitoring.core.WsMonitoringPropertiesReader;
import com.accenture.hybris.wsmonitoring.data.WsMonitoringData;


@Controller
public class WsMonitoringController
{
    //  @Resource(name = "wsMonitoringPropertiesReader")
    //  private static WsMonitoringPropertiesReader propertiesReader;

    //Client HTTP utilizzato per effettuare le richieste
    private final CloseableHttpClient httpclient;
    private final ConcurrentHashMap<String, String> servizi;
    private final static Logger LOG = Logger.getLogger(WsMonitoringController.class);

    private String accountBase64;
    private String username;
    private String password;

    public WsMonitoringController()
    {

        LOG.debug("WsMonitoringController---Inizio Costruttore");

        final WsMonitoringPropertiesReader propertiesReader = new WsMonitoringPropertiesReader();
        this.httpclient = HttpClients.createDefault();
        this.servizi = new ConcurrentHashMap<String, String>();
        final int numservizi = Integer.parseInt((propertiesReader.getPropertyValue("numero.servizi")));

        if (propertiesReader.getPropertyValue("basicauthentication.username") != null
                && propertiesReader.getPropertyValue("basicauthentication.password") != null)
        {
            this.username = propertiesReader.getPropertyValue("basicauthentication.username");
            this.password = propertiesReader.getPropertyValue("basicauthentication.password");
            LOG.debug("username: " + this.username + "   password: " + this.password);
            final String account = this.username + ":" + this.password;
            this.accountBase64 = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(account.getBytes());
        }
        LOG.debug("WsMonitoringController---trovati " + numservizi + " servizi");


        for (int i = 1; i <= numservizi; i++)
        {
            try
            {
                servizi.put(propertiesReader.getPropertyValue("servizio" + i + ".nome"),
                        propertiesReader.getPropertyValue("servizio" + i + ".url"));
                LOG.debug("WsMonitoringController---aggiunto servizio" + 1);
            }
            catch (final Exception e)
            {
                LOG.debug("servizio" + i + " non esistente");
            }
        }
        LOG.debug("WsMonitoringController---Fine Costruttore");
    }

    @RequestMapping(value = "/{nomeServizio:.*}")
    public String interrogaServizio(@PathVariable("nomeServizio") final String nomeServizio, final Model model)
    {
        LOG.debug("WsMonitoringController---Interrogazione servizio:" + nomeServizio);
        final String url = servizi.get(nomeServizio);

        if (null != url)
        {
            LOG.debug("WsMonitoringController---Interrogazione servizio: trovata url" + url);
            model.addAttribute("result", doRequest(nomeServizio, url, model));
        }
        else
        {
            LOG.debug("WsMonitoringController---Interrogazione servizio:servizio:" + nomeServizio + " non esistente");
            model.addAttribute("errore", nomeServizio);
        }

        return "pagina";
    }


    private WsMonitoringData doRequest(final String nome, final String url, final Model model)
    {

        WsMonitoringData wsData = null;
        final HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try
        {
            if (this.accountBase64 != null && nome.equals("alfresco")) /* 12-05-2014: aggiunta condizione su servizio alfresco che è l'unico con basicauthentication */
            {
                LOG.debug("accountBase64: " + accountBase64);
                httpget.addHeader("Authorization", accountBase64);
            }
            response = httpclient.execute(httpget);

            wsData = new WsMonitoringData(nome, url, response.getStatusLine().getStatusCode(), response.getStatusLine()
                    .getReasonPhrase());
            LOG.debug("Status code: " + wsData.getStatusCode());
            LOG.debug("Reason phrase: " + wsData.getReasonPhrase());
            LOG.debug("Service name: " + wsData.getServiceName());
            LOG.debug("url: " + wsData.getUrl());
            LOG.debug("result: " + wsData.getResult());

            model.addAttribute("result", wsData);
        }
        catch (final ClientProtocolException cex)
        {
            wsData = new WsMonitoringData(nome, url, 500, "ClientProtocolException");
            LOG.info("WsMonitoringController---doRequest: ClientProtocolException - Errore nella creazione della response :");
            cex.printStackTrace();
        }
        catch (final IOException e)
        {
            wsData = new WsMonitoringData(nome, url, 500, "IOException");
            LOG.info("WsMonitoringController---doRequest: IOException - Errore nell'esecuzione della GET :");
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (null != response)
                {
                    response.close();
                }
            }
            catch (final IOException e)
            {
                wsData = new WsMonitoringData(nome, url, 500, "IOException");
                LOG.info("WsMonitoringController---doRequest: IOException - Errore nella chiusura della response :");
                e.printStackTrace();
            }
        }
        return wsData;
    }




    public CloseableHttpClient getHttpclient()
    {
        return httpclient;
    }


}

使用的依赖项是:

commons-codec-1.9.jar
commons-logging-1.2.jar
fluent-hc-4.5.1.jar
httpclient-4.5.1.jar
httpclient-cache-4.5.1.jar
httpclient-win-4.5.1.jar
httpcore-4.4.3.jar
httpmime-4.5.1.jar
jna-4.1.0.jar
jna-platform-4.1.0.jar

Exception的堆栈是:

 ERROR [localhost-startStop-1] [DispatcherServlet] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wsMonitoringController' defined in file [C:\coop\hybris-commerce-suite-5.5.0.6\hybris\bin\custom\wsmonitoring\web\webroot\WEB-INF\classes\com\accenture\hybris\wsmonitoring\controllers\WsMonitoringController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.accenture.hybris.wsmonitoring.controllers.WsMonitoringController]: Constructor threw exception; nested exception is java.lang.NoSuchFieldError: INSTANCE
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1037)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:983)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.accenture.hybris.wsmonitoring.controllers.WsMonitoringController]: Constructor threw exception; nested exception is java.lang.NoSuchFieldError: INSTANCE
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1030)
        ... 29 more
Caused by: java.lang.NoSuchFieldError: INSTANCE
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
        at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
        at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)

1 个答案:

答案 0 :(得分:1)

这是问题所在:

  

创建文件中定义名称为'wsMonitoringController'的bean时出错... bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.accenture.hybris.wsmonitoring.controllers.WsMonitoringController]:构造函数抛出异常;嵌套异常是java.lang.NoSuchFieldError:INSTANCE

Spring正在寻找名为INSTANCE的字段,但它不在您的班级中。检查Spring配置文件并将其与WsMonitoringController对齐。

我认为您没有发布整个文件。