使用setter将对象转换为java中的map

时间:2016-10-13 11:05:41

标签: java object soap soap-client webservices-client

我需要创建一个RequestContext的映射,并将其作为标题在BindingProvider中设置。我目前的代码如下。我需要使用相应的setter创建一个映射,并将其作为header的put()方法的第二个参数传递。

RequestContext requestContextBody = new RequestContext();
requestContextBody.setVersion("1.4");
requestContextBody.setLanguage(Language.EN);
requestContextBody.setGroupID("111");
requestContextBody.setRequestReference("UserRef");
requestContextBody.setUserToken(new JAXBElement<String>(new QName("http://purolator.com/pws/datatypes/v1","UserToken"), String.class, "7a0f98ec-6e9f-4eb1-9fcb-40086e2c1040"));

URL wsdlURL = PurolatorClientImpl.class.getClassLoader().getResource("wsdl/EstimatingService.wsdl");
EstimatingService ss = new EstimatingService(wsdlURL, SERVICE_NAME);
EstimatingServiceContract port = ss.getEstimatingServiceEndpoint();

((BindingProvider) port).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestContextBody); //THIS IS WHERE I NEED TO PASS THE MAP IN PLACE OF THE REQUESTCONTEXTBODY OBJECT
((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "867aa05887b94f48988b6bf46340c5b3");
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "Ze@5l22N");

StackTrace ::

Exception in thread "main" javax.xml.ws.WebServiceException: java.lang.ClassCastException: com.purolator.pws.datatypes.v1.RequestContext cannot be cast to java.util.Map
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:207)
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:122)
    at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:95)
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:626)
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)
    at com.sun.xml.internal.ws.client.Stub.process(Stub.java:308)
    at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:163)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:98)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
    at com.sun.proxy.$Proxy26.getFullEstimate(Unknown Source)
    at com.pb.gcs.client.ClientMain.main(ClientMain.java:96)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassCastException: com.purolator.pws.datatypes.v1.RequestContext cannot be cast to java.util.Map
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:136)
    ... 18 more

1 个答案:

答案 0 :(得分:0)

javadocs MessageContext.HTTP_REQUEST_HEADERS中所述,需要java.util.Map

所以你可以试试这个:

Map context = ((BindingProvider) port).getRequestContext();
Map<String, List<String>> requestHeaders = (Map) context.get(MessageContext.HTTP_REQUEST_HEADERS) ;
requestHeaders.put("User-Agent", Collections.singletonList("Mozilla [...]"));
// add all yours setting
context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);