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

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

问题描述

我正在尝试使用提供商公开的 Web 服务.Provider 在他的最后有一个严格的检查,请求 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?

谢谢,纳文.

推荐答案

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

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

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

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

可能的问题

我相信 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.

根目录

package forum11215485;

import javax.xml.bind.annotation.*;

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

    String nullValue;
    String emptyStringValue;
    String stringValue;

}

演示

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

}

输出

注意没有为 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

以下 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;
    }

}

包信息

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.*;

输出

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

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天全站免登陆