Jax-WS - 从请求XML中删除空标记 [英] Jax-WS - To Remove Empty Tags from Request XML

查看:79
本文介绍了Jax-WS - 从请求XML中删除空标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用提供商公开的Web服务。提供者在他的结尾有一个严格的检查,请求xml不应该包含没有值的标签。

I'm trying to consume a web service exposed by a provider. The Provider has a strict checking at his end that the request xml should not contain tags which don't have values.

我正在使用Jax-WS。如果我没有在特定对象中设置值,则它将作为空标记发送,并且标记存在。 PFB这个例子说明了我的问题。

I'm using Jax-WS. If i don't set value in a particular object, it is being sent as empty tag and the tag is present. PFB the example illustrating my issue.

客户端XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:host="http://host.testing.webservice.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <host:testingMathod>
         <arg0>
            <PInfo>
               <IAge>45</IAge>
               <strName>Danny</strName>
            </PInfo>
            <strCorrId>NAGSEK</strCorrId>
            <strIpAddress></strIpAddress>
         </arg0>
      </host:testingMathod>
   </soapenv:Body>
</soapenv:Envelope>

在此,未给出IpAddress的值,因此发送了空标记。

In this, the value for IpAddress is not been given and hence the empty tag is been sent.

因此,请告诉我们要删除请求xml中的空标记需要做些什么。 Handlerchain是唯一的解决方案吗?

Hence kindly let us me know what needs to be done to remove the empty tags in request xml. Is Handlerchain is the only solution for the same?

谢谢,
Naveen。

Thanks, Naveen.

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

默认情况下,MOXy与其他JAXB实现一样,不会为空值编组元素:

By default MOXy like other JAXB implementations will not marshal an element for null values:

  • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html

可能出现的问题

我认为 strIpAddress 属性不为null,但包含空字符串()的值。这将导致空元素被写出。

I believe the strIpAddress property is not null, but contains a value of empty String (""). This would cause the empty element to be written out.

Root

Root

package forum11215485;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    String nullValue;
    String emptyStringValue;
    String stringValue;

}

演示

Demo

package forum11215485;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.nullValue = null;
        root.emptyStringValue = "";
        root.stringValue = "Hello World";

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

输出

Output

注意没有为 nullValue 字段编组的元素,以及 emptyStringValue 字段被编组为空元素。

Note how there is no element marshalled out for the nullValue field, and the emptyStringValue field is marshalled as an empty element.

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <emptyStringValue></emptyStringValue>
   <stringValue>Hello World</stringValue>
</root>

解决方案#1 - 确保该属性设置为null而不是

解决方案#2 - 编写一个将转换为null的XmlAdapter

XmlAdapter 是一种JAXB机制,允许将对象编组为另一个对象。

An XmlAdapter is a JAXB mechanism that allows an object to be marshalled as another object.

StringAdapter

StringAdapter

以下 XmlAdapter 将空字符串封送为null。这将导致它们不出现在XML表示中。

The following XmlAdapter will marshal empty strings as null. This will cause them not to appear in the XML representation.

package forum11215485;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class StringAdapter extends XmlAdapter<String, String> {

    @Override
    public String unmarshal(String v) throws Exception {
        return v;
    }

    @Override
    public String marshal(String v) throws Exception {
        if(null == v || v.length() == 0) {
            return null;
        }
        return v;
    }

}

包-info

package-info

使用 XmlAdapter > @XmlJavaTypeAdapter 注释。下面是一个在包级别挂钩的示例,以便它应用于包中String类型的字段/属性。有关详细信息,请参阅: http://blog.bdoughan。 com / 2012/02 / jaxb-and-package-level-xmladapters.html

The XmlAdapter is hooked in using the @XmlJavaTypeAdapter annotation. Below is an example of hooking it in at the package level so that it applies to a fields/properties of type String within the package. For more information see: http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html

@XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
package forum11215485;

import javax.xml.bind.annotation.adapters.*;

输出

Output

现在运行演示代码的输出如下:

Now the output from running the demo code is the following:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <stringValue>Hello World</stringValue>
</root>

这篇关于Jax-WS - 从请求XML中删除空标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆