我正在使用CXF 3.0.8来调用Web服务:
@WebService(targetNamespace = "http://tempuri.org/",name = "INotificationGateway") @XmlSeeAlso({ObjectFactory.class,com.microsoft.schemas._2003._10.serialization.ObjectFactory.class,org.datacontract.schemas._2004._07.notificationsgateway.ObjectFactory.class,com.microsoft.schemas._2003._10.serialization.arrays.ObjectFactory.class}) public interface INotificationGateway { @WebResult(name = "SendSMSResult",targetNamespace = "http://tempuri.org/") @Action(input = "http://tempuri.org/INotificationGateway/SendSMS",output = "http://tempuri.org/INotificationGateway/SendSMSResponse",fault = {@FaultAction(className = INotificationGatewaySendSMSEAICustomErrorFaultFaultMessage.class,value = "http://tempuri.org/INotificationGateway/SendSMSEAICustomErrorFault")}) @RequestWrapper(localName = "SendSMS",targetNamespace = "http://tempuri.org/",className = "org.tempuri.SendSMS") @WebMethod(operationName = "SendSMS",action = "http://tempuri.org/INotificationGateway/SendSMS") @ResponseWrapper(localName = "SendSMSResponse",className = "org.tempuri.SendSMSResponse") public org.datacontract.schemas._2004._07.notificationsgateway.NotificationResponse sendSMS( @WebParam(name = "SendSMSNotificationRequest") org.datacontract.schemas._2004._07.notificationsgateway.SendSMSNotificationRequest sendSMSNotificationRequest ) throws INotificationGatewaySendSMSEAICustomErrorFaultFaultMessage; }
我在服务请求中发送的对象是:
@XmlAccessorType(XmlAccesstype.FIELD) @XmlType(name = "SendSMSNotificationRequest",propOrder = { "iSarabic","mobileNo","refrenceId","templateCode","templateValues" }) public class SendSMSNotificationRequest { @XmlElementRef(name = "ISarabic",namespace = "http://schemas.datacontract.org/2004/07/NotificationsGateway",type = JAXBElement.class,required = false) protected JAXBElement<Boolean> iSarabic; @XmlElementRef(name = "MobileNo",required = false) protected JAXBElement<String> mobileNo; @XmlElementRef(name = "RefrenceId",required = false) protected JAXBElement<String> refrenceId; @XmlElementRef(name = "TemplateCode",required = false) protected JAXBElement<String> templateCode; @XmlElementRef(name = "TemplateValues",required = false) protected JAXBElement<arrayofstring> templateValues;
当我调用该服务时,soap body按如下方式发送,前缀为错误的xmlns =“”:
<MobileNo xmlns="" xmlns:ns5="http://schemas.datacontract.org/2004/07/NotificationsGateway">0000000000</MobileNo> <RefrenceId xmlns="" xmlns:ns5="http://schemas.datacontract.org/2004/07/NotificationsGateway">123</RefrenceId> <TemplateCode xmlns="" xmlns:ns5="http://schemas.datacontract.org/2004/07/NotificationsGateway">123</TemplateCode>
如果我删除xmlns =“”并在sopui中使用此soap请求,该服务工作正常,所以我想知道如何从请求中删除此xmlns =“”,我使用的代码如下:
JaxWsProxyfactorybean factorybean = new JaxWsProxyfactorybean(); factorybean.setServiceClass(INotificationGateway.class); factorybean.setAddress("https://localhost:4431/NotificationsGateway/NotificationGateway.svc"); INotificationGateway port = (INotificationGateway) factorybean.create(); Client client = ClientProxy.getClient(port); client.getininterceptors().add(new LoggingInInterceptor()); client.getoutInterceptors().add(new LoggingOutInterceptor()); HTTPConduit http = (HTTPConduit) client.getConduit(); http.getAuthorization().setUserName("myuser"); http.getAuthorization().setPassword("mypass"); TLSClientParameters tlsCP = new TLSClientParameters(); if (ignoreSSL) tlsCP.setTrustManagers(createIgnoredTrustManager()); tlsCP.setdisableCNCheck(true); http.setTlsClientParameters(tlsCP); Endpoint cxfEndpoint = client.getEndpoint(); Map<String,Object> outProps = new HashMap<String,Object>(); outProps.put(WSHandlerConstants.ACTION,WSHandlerConstants.USERNAME_TOKEN); outProps.put(WSHandlerConstants.USER,"myuser"); outProps.put(WSHandlerConstants.PASSWORD_TYPE,WSConstants.PW_TEXT); outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,ClientPasswordCallback.class.getName()); WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); cxfEndpoint.getoutInterceptors().add(wssOut); port.sendSMS(sendSMSNotificationRequest);
请告知如何解决这个问题,谢谢.
解决方法
默认情况下,JAX-WS(以及CXF)生成不合格的模式和消息.
因此,只有根元素是名称空间限定的,而子元素是
不是.如果将namespace属性添加到@WebParam,那就可以了
为该参数更改它.
因此,只有根元素是名称空间限定的,而子元素是
不是.如果将namespace属性添加到@WebParam,那就可以了
为该参数更改它.
或者,添加package-info.java,例如:
@javax.xml.bind.annotation.XmlSchema( namespace = "http://apache.org/hello_world_soap12_http/types",elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
强迫元素成为合格的形式.