如何从xml请求中删除xmlns =“”

时间:2016-04-14 13:02:42

标签: java web-services soap cxf jaxb2

我正在使用 CXF 3.0.8 来调用网络服务:

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

}

我在服务请求中发送的对象是:

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

当我打电话给服务时,肥皂体发送如下,前缀错误 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);

请告知如何解决这个问题,谢谢。

2 个答案:

答案 0 :(得分:0)

默认情况下,JAX-WS(以及CXF)会生成不合格的模式和消息 因此,只有根元素是名称空间限定的,而子元素是 不是。如果将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) 

强制元素成为合格的形式。

答案 1 :(得分:-1)

我们可以使用interceptors从请求中删除不需要的 xmlns =“”