Java 如果从服务器调用 Web 服务客户端失败:如果从 Junit(本地)调用,则调用成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20728188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:49:26  来源:igfitidea点击:

Webservice client fails if invoked from server : Gets called successfully if invoked from Junit(locally)

javaspringweb-serviceshttpjakarta-ee

提问by Kshitij Jain

I am working in jboss 7 and using CXF framework to build webservice client.

我在 jboss 7 工作并使用 CXF 框架来构建 web 服务客户端。

I have written a particular webservice client to call a webservice deployed in remote server.

我编写了一个特定的 web 服务客户端来调用部署在远程服务器中的 web 服务。

If this webservice client is invoked from a Junit test case, then webservice call is successful and we get the valid xml response from the server.

如果这个 webservice 客户端是从 Junit 测试用例调用的,那么 webservice 调用成功,我们从服务器获得有效的 xml 响应。

But if the same webservice client is invoked from the server(webservice client itself is deployed as webapp) then, it throws below exception :

但是,如果从服务器调用相同的 webservice 客户端(webservice 客户端本身部署为 webapp),那么它会抛出以下异常:

Caused by: javax.xml.ws.soap.SOAPFaultException: Could not find conduit initiator for address: 
                                http://10.100.20.101/metavante1/ConnectwareWS/DPAcctTxnInqWSV1
                         and transport: http://schemas.xmlsoap.org/soap/http
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
        at sun.proxy.$Proxy137.dpAcctTxnInq(Unknown Source)
        at webservice.client.hsa.MetavanteHSAPort.getAccountDetails(MetavanteHSAPort.java:1785) [webserviceclient.jar:]
        ... 34 more
Caused by: java.lang.RuntimeException: Could not find conduit initiator for address: 
                                http://10.100.20.101/metavante1/ConnectwareWS/DPAcctTxnInqWSV1
                         and transport: http://schemas.xmlsoap.org/soap/http
        at org.apache.cxf.binding.soap.SoapTransportFactory.getConduit(SoapTransportFactory.java:225)
        at org.apache.cxf.endpoint.AbstractConduitSelector.getSelectedConduit(AbstractConduitSelector.java:110)
        at org.apache.cxf.endpoint.UpfrontConduitSelector.prepare(UpfrontConduitSelector.java:63)
        at org.apache.cxf.endpoint.ClientImpl.prepareConduitSelector(ClientImpl.java:850)
        at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:525)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)
        at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
        ... 36 more

In this case the URL http://10.100.20.101/metavante1/ConnectwareWS/DPAcctTxnInqWSV1is picked up from a spring bean which is set in an XML file.

在这种情况下,URLhttp://10.100.20.101/metavante1/ConnectwareWS/DPAcctTxnInqWSV1是从在 XML 文件中设置的 spring bean 中获取的。

Below is the code for webservice client :

下面是 webservice 客户端的代码:

public HSAAccountDetailsBn getAccountDetails(String bankAccNum)
            throws ServiceException
    {
        HSAAccountDetailsBn detailsBn = new HSAAccountDetailsBn();

        try{
          URL wsdl = WebServiceClientUtil.getResourceAsURL("wsdl/clients/Metavante/DPAcctTxnInqWSV1.wsdl");
          MtvnCWDPAcctTxnInqWSV1 service = new MtvnCWDPAcctTxnInqWSV1(wsdl);
          MtvnCWDPAcctTxnInqWSV1Interface port = service
                    .getMtvnCWDPAcctTxnInqWSV1Port();

          BindingProvider provider = (BindingProvider) port;
          provider.getRequestContext().put(
          BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
          this.getServiceEndpointAcctTxnInq()
          );

          System.out.println("Invoking dpAcctTxnInq...");

          webservice.client.metavante.deposit.acctTrxnEnquiry.ObjectFactory obj = new webservice.client.metavante.deposit.acctTrxnEnquiry.ObjectFactory();

          DPAcctTxnInqMtvnSvcReq dpAcctTxnInqReq = obj
                    .createDPAcctTxnInqMtvnSvcReq();
          dpAcctTxnInqReq.setMtvnSvcVer("1.0");
          dpAcctTxnInqReq.setMsgUUID("11111111-1111-1111-1111-111111111114");

          DPAcctTxnInqMtvnSvcReq.PrcsParms prcsParms = obj
                    .createDPAcctTxnInqMtvnSvcReqPrcsParms();

          prcsParms.setSrcID("GJL747");
          prcsParms.setTestInd("Y");          
          dpAcctTxnInqReq.setPrcsParms(prcsParms);

          DPAcctTxnInqMtvnSvcReq.Svc svc = obj
                    .createDPAcctTxnInqMtvnSvcReqSvc();

          DPAcctTxnInqMtvnSvcReq.Svc.SvcParms svcParms = obj
                    .createDPAcctTxnInqMtvnSvcReqSvcSvcParms();

          svcParms.setApplID("DP");
          svcParms.setRoutingID(getRoutingId());
          svcParms.setRqstUUID("11111111-1111-1111-1111-111111111114");
          svcParms.setSvcID("DPAcctTxnInq");
          svcParms.setSvcVer("1.0");
          svc.setSvcParms(svcParms);

          DPAcctTxnInqMtvnSvcReq.Svc.Security security = obj
                    .createDPAcctTxnInqMtvnSvcReqSvcSecurity();

          DPAcctTxnInqMtvnSvcReq.Svc.Security.BasicAuth basicAuth = obj
                    .createDPAcctTxnInqMtvnSvcReqSvcSecurityBasicAuth();

          basicAuth.setUsrID(this.getUsernameDeposit());
          basicAuth.setPwd(this.getPasswordDeposit());
          security.setBasicAuth(basicAuth);
          svc.setSecurity(security);

          DPAcctTxnInqMtvnSvcReq.Svc.MsgData msgData = obj
                    .createDPAcctTxnInqMtvnSvcReqSvcMsgData();
          DPAcctTxnInqReqData dpAcctInqReqData = obj
                    .createDPAcctTxnInqReqData();

          if (!CommonSupportUtil.isNull(bankAccNum))
          {
              dpAcctInqReqData.setE20007(bankAccNum); // account number
          }

          dpAcctInqReqData.setE310129("N");
          dpAcctInqReqData.setE310130("N");
          dpAcctInqReqData.setE310128("Y");

          msgData.setDPAcctTxnInqReqData(dpAcctInqReqData);
          svc.setMsgData(msgData);

          dpAcctTxnInqReq.getSvc().add(svc);

          DPAcctTxnInqMtvnSvcRes dpAcctTxnInqRes = port
                    .dpAcctTxnInq(dpAcctTxnInqReq);

          List<DPAcctTxnInqMtvnSvcRes.Svc> resSvclist = dpAcctTxnInqRes.getSvc();
          DPAcctTxnInqMtvnSvcRes.Svc resSvc = resSvclist.get(0);
          DPAcctTxnInqResData resData = resSvc.getMsgData()
                    .getDPAcctTxnInqResData();

          String returnStatus = dpAcctTxnInqRes.getErrCde();

          if (returnStatus.equalsIgnoreCase("0"))
            {
                detailsBn.setAccruedInterest(new Double(resData.getE20638()));
                detailsBn.setAvailableBalance(new Double(resData.getE21667()));
                detailsBn.setClosingBalance(new Double(resData.getE20167()));
                detailsBn.setCurrentBalance(new Double(resData.getE21661()));
                detailsBn.setLedgerBalance(new Double(resData.getE21632()));

                String openIndicator = resData.getE20276();
                detailsBn.setOpenIndicator(openIndicator);
                String dormantIndicator = resData.getE20277();
                detailsBn.setDormantIndicator(dormantIndicator);

            }
            else
            {
                throw new ServiceException("Could not access Metavante Deposit system ");
            }

        }
        catch (SOAPFaultException e) {
            throw new ServiceException(
                    "Vendor is intercepting the request but it is not returning the data. "
                            + "This can mean that either they are not servicing request at this moment "
                            + "at all or they are unable to service this request.",
                    e);

        } 

        return detailsBn;

    }//Method ends

I looked some previous post on these type of issues, but none of them properly address my problem.

我查看了之前关于这些类型问题的一些帖子,但没有一个能正确解决我的问题。

I am not able to figure out the difference between the webservice client invocation from Junit vs jboss application server.

我无法弄清楚来自 Junit 与 jboss 应用程序服务器的 web 服务客户端调用之间的区别。

回答by Roberto Vergallo

Probably, JUnit uses Eclipse's embedded JRE, while your application may use a specific JRE installed in your system. Check that you use the same JRE both for executing your application and JUnit test cases. Then, I had the same exception in my project, in my case the problem was that I wrote the web service url within "..." in my configuration file. Hope it helps, Roberto

可能 JUnit 使用 Eclipse 的嵌入式 JRE,而您的应用程序可能使用安装在系统中的特定 JRE。检查您是否使用相同的 JRE 来执行应用程序和 JUnit 测试用例。然后,我在我的项目中遇到了同样的异常,就我而言,问题是我在配置文件中的“...”中写入了 Web 服务 url。希望它有所帮助,罗伯托

回答by Daniil Shevelev

Adding dependency on cxf-rt-transports-http-jettysolved it for me, as suggested here.

添加cxf-rt-transports-http-jetty对我的依赖解决了它,如建议here