gpt4 book ai didi

java - 使用 Apache CXF 调用 SOAP API 时发生 ClassCastException

转载 作者:行者123 更新时间:2023-11-30 08:26:34 27 4
gpt4 key购买 nike

我正在使用 Maven、Java 1.6、Apache CXF。在某些类的 Apache CXF 中调用 Soap API 时,我得到了一些类转换异常。这是堆栈跟踪。

javax.xml.ws.soap.SOAPFaultException: java.net.URL cannot be cast to java.lang.String    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:157)    at com.sun.proxy.$Proxy83.importCapturedData(Unknown Source)    at com.flipkart.dynamics.api.DynamicsClientService.handleDataLoadRequest(DynamicsClientService.java:109)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480)    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411)    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)    at com.flipkart.recon.api.filter.ContentTypeFilter.doFilter(ContentTypeFilter.java:73)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)    at java.lang.Thread.run(Thread.java:695)Caused by: java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String    at org.apache.cxf.endpoint.AbstractConduitSelector.getSelectedConduit(AbstractConduitSelector.java:99)    at org.apache.cxf.endpoint.UpfrontConduitSelector.prepare(UpfrontConduitSelector.java:63)    at org.apache.cxf.endpoint.ClientImpl.prepareConduitSelector(ClientImpl.java:886)    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:560)    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:474)    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:377)    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:330)    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)    ... 41 more[2014-01-28 16:07:44.572] ERROR [http-bio-8082-exec-2][ContentTypeFilter][ContentTypeFilter.java:93] Unhandled Exception. javax.xml.ws.soap.SOAPFaultException: java.net.URL cannot be cast to java.lang.String    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:157) ~[cxf-2.7.8.jar:2.7.8]    at com.sun.proxy.$Proxy83.importCapturedData(Unknown Source) ~[na:na]    at com.flipkart.dynamics.api.DynamicsClientService.handleDataLoadRequest(DynamicsClientService.java:109) ~[DynamicsClientService.class:na]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_65]    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_65]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_65]    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_65]    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350) ~[jersey-server-1.14.jar:1.14]    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) ~[jersey-servlet-1.14.jar:1.14]    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) ~[jersey-servlet-1.14.jar:1.14]    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) ~[jersey-servlet-1.14.jar:1.14]    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) ~[servlet-api.jar:na]    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.47]    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.47]    at com.flipkart.recon.api.filter.ContentTypeFilter.doFilter(ContentTypeFilter.java:73) ~[ContentTypeFilter.class:na]    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.47]    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.47]    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.47]    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.47]    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:7.0.47]    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.47]    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) [catalina.jar:7.0.47]    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [catalina.jar:7.0.47]    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.47]    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.47]    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) [tomcat-coyote.jar:7.0.47]    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) [tomcat-coyote.jar:7.0.47]    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) [tomcat-coyote.jar:7.0.47]    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [na:1.6.0_65]    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [na:1.6.0_65]    at java.lang.Thread.run(Thread.java:695) [na:1.6.0_65]Caused by: java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String    at org.apache.cxf.endpoint.AbstractConduitSelector.getSelectedConduit(AbstractConduitSelector.java:99) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.endpoint.UpfrontConduitSelector.prepare(UpfrontConduitSelector.java:63) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.endpoint.ClientImpl.prepareConduitSelector(ClientImpl.java:886) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:560) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:474) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:377) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:330) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96) ~[cxf-2.7.8.jar:2.7.8]    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135) ~[cxf-2.7.8.jar:2.7.8]    ... 41 common frames omitted

Here is the code that I am using:

try
{
wsdlURL =
new URL("some http url with requiring basic authentication");
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(wsdlURL);

OPSRequestManagement opsRequestManagement = new OPSRequestManagement(wsdlURL, SERVICE_NAME);
OPSRequestManagementPort opsRequestManagementPort = opsRequestManagement.getOPSRequestManagementPort();

Map<String, Object> req_ctx = ((BindingProvider) opsRequestManagementPort).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsdlURL);

Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("domain\\username"));
headers.put("Password", Collections.singletonList("password"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

boolean resp;

List<CapturedData> capturedDataList = dynamicsDao.getCapturedData(startDate.getTime(), endDate.getTime());
FKPG fkpg = new FKPG();
fkpg.getContent().addAll(capturedDataList);
resp = opsRequestManagementPort.importCapturedData(fkpg);

最佳答案

@sadhu 的回答为我指明了解决我自己问题的正确方向,但对我来说太神秘了。

拿这个片段:

wsdlURL = new URL("some http url with requiring basic authentication");

这告诉我们 wsdlURL 是 java.net.URL 类型。接下来我们找到这个:

Map<String, Object> req_ctx = ((BindingProvider) opsRequestManagementPort).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsdlURL);

要解决此问题,请将最后一行更改为:

req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsdlURL.toExternalForm());

重点是您应该在 map 中放置一个 String 对象,而不是 URL 实例。


为防止将来发生这种情况,请始终检查 Javadoc。这就是关于 ENDPOINT_ADDRESS_PROPERTY 的内容: (强调我的)

Standard property: Target service endpoint address. The URI scheme for the endpoint address specification must correspond to the protocol/transport binding for the binding in use.

Type: java.lang.String

关于java - 使用 Apache CXF 调用 SOAP API 时发生 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21403378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com