我有一个使用该4.3.6
版本的 Spring Web 应用程序。直到某个时候,它都适用于 XML 和 JSON。
- 对于 Json,我使用 Jackson。
- 对于我曾经使用过的 XML
JAXB2
,但现在不再使用了,因为它不支持通用集合
通用集合表示为:
public class GenericCollection<T> {
private Collection<T> collection;
public GenericCollection(){
this.collection = new LinkedHashSet<>();
}
public GenericCollection(Collection<T> collection){
this.collection = collection;
}
...
因此,对于从jackson-dataformat-xml项目XML
转移JABX2
到Jackson
使用的应用程序
要序列化一个Date
for JSON,我使用:
public class JsonDateSerializer extends JsonSerializer<Date> {
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* {@inheritDoc}
*/
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(value);
gen.writeString(formattedDate);
}
}
并使用如何:
@JsonProperty("fecha")
@JsonSerialize(using=JsonDateSerializer.class)
public Date getFecha() {
return fecha;
}
对于 XML,当被JAXB2
使用时,再次对Date
序列化使用以下内容:
public class XmlDateAdapter extends XmlAdapter<String, Date> {
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* {@inheritDoc}
*/
@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.parse(v);
}
/**
* {@inheritDoc}
*/
@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}
}
并使用如何:
@JsonProperty("fecha")
@JsonSerialize(using=JsonDateSerializer.class)
@XmlElement(name="fecha")
@XmlJavaTypeAdapter(XmlDateAdapter.class)
public Date getFecha() {
return fecha;
}
由于这种从Jaxb2
到的迁移,jackson-dataformat-xml
我知道后者忽略了JAXB2
注释。因此现在与:
@JsonProperty("fecha")
@JsonSerialize(using=JsonDateSerializer.class)
@JacksonXmlProperty(localName="fecha")
@XmlJavaTypeAdapter(XmlDateAdapter.class)
public Date getFecha() {
return fecha;
}
因此从@XmlElement(name="fecha")
到@JacksonXmlProperty(localName="fecha")
现在的问题是也@XmlJavaTypeAdapter(XmlDateAdapter.class)
被忽略了。
问题:
@XmlJavaTypeAdapter
from JAXB2
to的等价注解是什么jackson-dataformat-xml