2016-04-14 148 views
5

我使用CXF 3.0.8調用Web服務:如何刪除的xmlns =「」從XML請求

@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", targetNamespace = "http://tempuri.org/", 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; 

} 

和目標i的服務請求發送是:

@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", namespace = "http://schemas.datacontract.org/2004/07/NotificationsGateway", type = JAXBElement.class, required = false) 
    protected JAXBElement<String> mobileNo; 
    @XmlElementRef(name = "RefrenceId", namespace = "http://schemas.datacontract.org/2004/07/NotificationsGateway", type = JAXBElement.class, required = false) 
    protected JAXBElement<String> refrenceId; 
    @XmlElementRef(name = "TemplateCode", namespace = "http://schemas.datacontract.org/2004/07/NotificationsGateway", type = JAXBElement.class, required = false) 
    protected JAXBElement<String> templateCode; 
    @XmlElementRef(name = "TemplateValues", namespace = "http://schemas.datacontract.org/2004/07/NotificationsGateway", type = JAXBElement.class, required = false) 
    protected JAXBElement<ArrayOfstring> templateValues; 

當我調用服務,SOAP主體被髮送如下,錯的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); 

請指教如何解決,謝謝。

+0

您可以按照教程https://recalll.co/app/?q=How%20to%20remove%20namespace%20in%20XML%20through%20java? – SkyWalker

回答

0

默認情況下,JAX-WS(以及CXF)會生成未限定的模式和消息。
因此,只有根元素是名稱空間限定的,子元素不會是 。如果您將名稱空間屬性添加到@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) 

迫使元素融入限定形式。

相關問題