Java 带有空字段的 JAXB 编组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB Marshalling with null fields
提问by Jalpesh
This is a pretty simple request, but I just didn't find a way to do it.
这是一个非常简单的请求,但我只是没有找到方法来做到这一点。
I'm basically trying to set up a role in JAXB which says that whenever an null field is encountered, instead of ignoring it in the output, set it to an empty value. So for the class :
我基本上是在尝试在 JAXB 中设置一个角色,它表示每当遇到空字段时,不要在输出中忽略它,而是将其设置为空值。所以对于班级:
@XMLRootElement
Class Foo {
Integer num;
Date date;
….
}
When this has been marshalled into the XML file if the date field is null, my output does not have that element in it. What I want to do is include all the fields in the output; and if they are null, replace them with - say a blank. So the output should be :
如果日期字段为空,则将其编组到 XML 文件中时,我的输出中不包含该元素。我想要做的是在输出中包含所有字段;如果它们为空,请将它们替换为 - 说一个空白。所以输出应该是:
<foo>
<num>123</num>
<date></date>
</foo>
Thanks,
谢谢,
Jalpesh.
贾尔佩什。
回答by Chris Jester-Young
But but but...a empty string is not a valid lexical representation for a date, so you can't do that. i.e., if you generated an XML document with an empty value for a date field, it won't validate properly.
但是但是……空字符串不是日期的有效词法表示,因此您不能这样做。即,如果您生成的 XML 文档的日期字段值为空,它将无法正确验证。
In other words, if your date
element has a minOccurs
of 1 or more and not nillable
, then you absolutely must have (1 or more) dates, which can't be null (or blanks, or other non-values).
换句话说,如果您的date
元素的 aminOccurs
为 1 或更多而不是nillable
,那么您绝对必须有(1 个或更多)日期,日期不能为空(或空白或其他非值)。
回答by Chris Dail
As indicated in the other answer is invalid since it is not a valid date. I had a similar issue where I wanted to handle (same as ) specially. Since you cannot use null, you can use the default value mechanism in JAXB. The following will default the value if none is specified. You can through code detect this special date and handle this exception case.
如另一个答案所示,它无效,因为它不是有效日期。我有一个类似的问题,我想特别处理(与 相同)。由于不能使用 null,因此可以使用 JAXB 中的默认值机制。如果没有指定,以下将默认值。您可以通过代码检测此特殊日期并处理此异常情况。
@XmlElement(defaultValue="1970-01-01T00:00:00.0-00:00")
So it is possible to detected and empty date value but you just cannot use null to do it.
因此,可以检测到并清空日期值,但您不能使用 null 来执行此操作。
回答by Jalpesh
Thanks guys for your answers.
谢谢你们的回答。
Chris Dail - I tried your approach, and it didn't really do what I wanted. JAXB was still ignoring my null values, in spite of defining a default value for my fields.
Chris Dail - 我尝试了你的方法,但它并没有真正达到我想要的效果。尽管为我的字段定义了默认值,但 JAXB 仍然忽略我的空值。
I did stumble across the answer after somebody in the Jersey forums pointed me to documentation section 2.2.12.8 No Value.
在 Jersey 论坛上有人向我指出文档部分2.2.12.8 No Value之后,我确实偶然发现了答案。
Basically, all I had to do was to add the following to my fields :
基本上,我所要做的就是将以下内容添加到我的字段中:
@XmlElement(nillable = true)
Once I added that, JAXB would show up those fields when marshalling them to XML like this:
添加后,JAXB 将在将这些字段编组为 XML 时显示这些字段,如下所示:
...
<num>5</num>
<date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
....
回答by Jan Serneels
In MOXy u can specify how the jsonProvider must do its job for JAXB.
在 MOXy 中,您可以指定 jsonProvider 必须如何为 JAXB 完成其工作。
So when doing JAX-RS, add following code in your class derived from Application
因此,在执行 JAX-RS 时,在从 Application 派生的类中添加以下代码
I used this code on Tomcat 7 with good results. (eclipselink 2.4.1)
我在 Tomcat 7 上使用了这段代码,结果很好。(eclipselink 2.4.1)
@ApplicationPath("/rest")
public class RestApplication extends Application
{
...
public Set< Object> getSingletons()
{
HashSet<Object> set = new HashSet<Object>(1);
set.add( newMoxyJsonProvider());
return set;
}
public static MOXyJsonProvider newMoxyJsonProvider()
{
MOXyJsonProvider result = new MOXyJsonProvider();
//result.setAttributePrefix("@");
result.setFormattedOutput( false);
result.setIncludeRoot( false);
result.setMarshalEmptyCollections( true);
//result.setValueWrapper("$");
return result;
}
On Glassfish 3.1.2 and WAS 8.5 however, newMoxyJsonProvider() is not needed, but then the JAXB provider gets configured by the server. In the case of Glassfish, which comes with MOXy, i witnessed same problems with null values. Did not check yet, but guess the answer is in configuring JAXB at application server level if possible at all.
然而,在 Glassfish 3.1.2 和 WAS 8.5 上,不需要 newMoxyJsonProvider(),但随后服务器配置了 JAXB 提供程序。在 MOXy 附带的 Glassfish 的情况下,我目睹了与空值相同的问题。还没有检查,但如果可能的话,猜测答案是在应用程序服务器级别配置 JAXB。
回答by xudd
Try this:
尝试这个:
marshal.setListener(new MarshallerListener());
with
和
public class MarshallerListener extends Marshaller.Listener {
public static final String BLANK_CHAR = "";
@Override
public void beforeMarshal(Object source) {
super.beforeMarshal(source);
Field[] fields = source.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
try {
if (f.getType() == String.class && f.get(source) == null) {
f.set(source, BLANK_CHAR);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}