将 object 与 setter 转换为 java 中的映射

Converting object with setter to map in java

我需要创建 RequestContext 的映射并将其作为 header 在 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");

堆栈跟踪::

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

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);