Saturday, April 23, 2016

How to remove xmlns=“” from xml request

Leave a Comment

i am using CXF 3.0.8 to invoke a web service:

@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;  } 

and the object i send in the service request is:

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

when i call the service, the soap body is sent as follows, prefixed with wrong 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> 

if i removed the xmlns="" and used this soap request in sopui, the service works fine, so i want to know how to remove this xmlns="" from the request, the code i am using is as follows:

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

please advise how to fix that, thanks.

2 Answers

Answers 1

By default, JAX-WS (and thus CXF) produces unqualified schemas and messages.
Thus, only the root element is namespace qualified and the sub elements would not be. If you add the namespace attribute to the @WebParam, that would change it for that parameter.

Alternatively, add a package-info.java with something like:

@javax.xml.bind.annotation.XmlSchema(        namespace = "http://apache.org/hello_world_soap12_http/types",      elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)  

to force the elements into qualified form.

Answers 2

We can use interceptors to remove the unwanted xmlns="" from the request.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment