- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此类的实例是大型对象图的一部分,不在对象图的根部:
public class Day
{
public Day(LocalDate date, List<LocalTime> times)
{
this.date = date;
this.times = times;
}
public Day()
{
this(null, null);
}
public LocalDate getDate()
{
return date;
}
public List<LocalTime> getTimes()
{
return times;
}
private final LocalDate date;
private final List<LocalTime> times;
}
使用 Jersey 和 JAXB 将对象图转换为 JSON。我为 LocalDate
和 LocalTime
注册了 XmlAdapter
。
问题是它只对 date
属性有效,对 times
属性无效。我怀疑这与 times
是一个列表而不是单个值这一事实有关。那么,我如何告诉 Jersey/JAXB 使用已注册的 XmlAdapter
编码 times
列表中的每个元素?
更新:
我通过添加标量 LocalTime
属性并观察 JSON 中的预期输出,确认 LocalTime
编码确实适用于标量 LocalTime
属性.
为了完整起见,这里是 package-info.java:
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value = LocalDateAdapter.class, type = LocalDate.class),
@XmlJavaTypeAdapter(value = LocalTimeAdapter.class, type = LocalTime.class)
})
package same.package.as.everything.else;
本地日期适配器
:
public class LocalDateAdapter extends XmlAdapter<String, LocalDate>
{
@Override
public LocalDate unmarshal(String v) throws Exception
{
return formatter.parseLocalDate(v);
}
@Override
public String marshal(LocalDate v) throws Exception
{
return formatter.print(v);
}
private final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd");
}
本地时间适配器
:
public class LocalTimeAdapter extends XmlAdapter<String, LocalTime>
{
@Override
public LocalTime unmarshal(String v) throws Exception
{
return formatter.parseLocalTime(v);
}
@Override
public String marshal(LocalTime v) throws Exception
{
return formatter.print(v);
}
private final DateTimeFormatter formatter = DateTimeFormat.forPattern("HHmm");
最佳答案
类的 XmlAdapter
应用于该类型的映射字段/属性,在集合的情况下,应用于集合中的每个项目。下面的例子证明这是有效的。您是否尝试过将示例独立运行到 XML 以验证这种映射。怀疑问题出在 XmlAdapter
以外的其他问题。
字符串适配器
以下 XmlAdapter
将在解码操作时将 String
转换为小写,并在编码时将其转换为大写。
package forum14569293;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class StringAdapter extends XmlAdapter<String, String> {
@Override
public String unmarshal(String v) throws Exception {
return v.toLowerCase();
}
@Override
public String marshal(String v) throws Exception {
return v.toUpperCase();
}
}
包信息
正如您的问题一样,包级 @XmlJavaTypeAdapters
注释将用于注册 XmlAdapter
。这将为这个包中的所有映射的 XmlAdapter
属性注册这个 String
(参见:http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html)。
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
})
package forum14569293;
import javax.xml.bind.annotation.adapters.*;
根
下面是一个示例域模型,类似于您的 Day
类,具有两个映射属性。第一个是 String
类型,第二个是 List<String>
。关于您的 Day
类,我注意到的一件事是您只有 get
方法。这意味着您将需要为 JAXB impl 添加 @XmlElement
注释,以将其视为映射属性。
package forum14569293;
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Root {
public Root(String foo, List<String> bar) {
this.foo = foo;
this.bar = bar;
}
public Root() {
this(null, null);
}
@XmlElement
public String getFoo() {
return foo;
}
@XmlElement
public List<String> getBar() {
return bar;
}
private final String foo;
private final List<String> bar;
}
演示
package forum14569293;
import java.util.*;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
List<String> bar = new ArrayList<String>(3);
bar.add("a");
bar.add("b");
bar.add("c");
Root root = new Root("Hello World", bar);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
输出
下面是运行演示代码的输出,我们看到所有字符串都被 XmlAdapter
转换为大写。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<bar>A</bar>
<bar>B</bar>
<bar>C</bar>
<foo>HELLO WORLD</foo>
</root>
更新
Thanks. I tried it and the XML consisted of one empty tag only, meaning there's something about the POJO model that JAXB doesn't like. (Perhaps it should be Serializable?)
JAXB 不要求 POJO 实现 Serializable
。
That's interesting because it seems to indicate that the only part JAXB plays in this is to lend its annotations and some other interfaces (e.g. XmlAdapter) to the JSON (de)serializer and that's where the relationship ends.
这取决于什么被用作 JSON 绑定(bind)层。 JAXB (JSR-222) 规范不包括 JSON 绑定(bind),因此这种类型的支持超出了规范。 EclipseLink JAXB (MOXy) 提供 native JSON 绑定(bind)(我是 MOXy 的负责人),您可以使用它执行以下操作:
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.marshal(root, System.out);
并获得以下将 XmlAdapter
考虑在内的输出:
{
"bar" : [ "A", "B", "C" ],
"foo" : "HELLO WORLD"
}
Nevertheless, when I get an opportunity I will do what I can to get JAXB to generate the XML and that may reveal something else.
这会很有用,因为我不相信您看到的是 JAXB 问题,而是您正在使用的 JSON 绑定(bind)层中的问题。
关于java - JAXB XmlAdapter 如何用于编码列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14569293/
我有一个基于 XML 的数据库,并且定义了一个用户模型,其中包含对角色(另一个模型)的引用列表。我已将 XMLAdapter 附加到 Roles 属性以自动填充角色。为此,我已在此适配器中 @auto
我正在使用 JAXB(以及一些用于 XSLT 的 Saxon)解码 XML 数据。在我的 XML 文档中,我有一个日期字符串值。 我做了一些研究并尝试了 XMLAdapter 和 @XMLJavaTy
我将 CXF 2.4 与 JAXB 结合使用。 我可以为我的 owm 类(例如 LWDate)的所有实例设置一个全局 XmlAdapter 吗? 我写了一个类: public class LWDate
有没有办法在 XmlAdapter 中检索当前的基本 URI?或者这通常是如何实现的? public class Service{ ... @GET public MyEntity g
我正在尝试让 XmlAdapter 到 HashMap 工作,但我一直收到异常。我关注了this blog entry非常接近,我已经多次检查我的代码,但我没有看到问题。 我正在使用最新版本的 org
我需要生成一个 XML使用以下格式的文件 JAXB2 , 它同时具有固定 和可变 xml 内容。 约束是什么? 变量的内容XML零件应该是 5 个不同的 XML schema 之一(计划让 JAXB2
此类的实例是大型对象图的一部分,不在对象图的根部: public class Day { public Day(LocalDate date, List times) {
我正在制作一个应用程序,它将包含 XML 文件中的数据。 我现在遇到一个问题:JAXB 不编码我的子类,因此当我编码 XML 文件时,所有对象都是父类的对象。我尝试了 @XMLSeeAlso、Acce
我想转换我的 payaload 中的项目列表,并将它们转换为 HashMap 。基本上,我拥有的是一个 Item xml 表示,其中包含一个 ItemID 列表。每个 ItemID 中都有一个 idT
当我试图直接解码一个根对象时,我遇到了 jaxb 的 XMLAdapters 的限制,而不是它是另一个对象中的一个字段,因此绕过了@XmlJavaTypeAdapter 我宁愿不包装我的对象,因为这会
我有一个外部(即不可修改的)com.external.Money 类,它有一个带有 getter 和 setter 的 java.util.Currency 字段。在我的 CXF jaxws 网络服务
我正在使用 JAXB XMLadapter 来编码和解码 boolean 值。应用程序的 XML 文件也将由 C# 应用程序访问。我们必须验证此 XML 文件,这是使用 XSD 完成的。 C# 应用程
我必须生成一个 xml 元素,该元素的值可以是任何“原始类型”(xsd:string、xsd:boolean 等)。示例: String Value 2011-10-21 2011-10-21T12:
有了这个 Jaxb Xml 定义,我尝试通过添加 @XmlPath(".") 来删除 Map Elements Wrapper但在解码期间会导致异常 @XmlRootElement public ab
我想使用 EclipseLink MOXy 将对象树持久保存为 XML。但我希望使用自定义的 DoubleAdapter 来编写所有 double 类型的字段。我知道我可以使用基于包的注释来做到这一点
我正在尝试创建一个 @XmlAdapter 以将混合列表中的单词拆分为名为 Word 的 Wrapper 类的对象。该字段还可能包含名为 Group 的 Word 扩展的实例。 这个想法是这样的: S
我需要为特定的 XmlAdapter 安装 DefaultValidationEventHandler。从我读过的内容来看,你只能这样做: JAXBContext context = JAXBCont
我正在使用 jaxbMarshaller 为第三方库类生成 xml。由于将 Calendar 对象转换为字符串的库 XmlAdapter 不使用 TimeZone 字段,因此编码器为 pojo 类的每
如何将 EJB 注入(inject)到 XmlAdapter 中? 我的想法是,我想通过 REST API 获取 ID 列表,并将该 ID 数组转换为实体对象的对象列表。例如: public clas
我正在使用以下源代码执行一个 Maven 项目 package com.coderplus.jaxb; import java.util.HashMap; import javax.xml.bind.
我是一名优秀的程序员,十分优秀!