我有一个 Spring Web 应用程序4.3.6
版本。在某种程度上,它可以与 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;
}
...
因此对于XML
该应用程序移自JABX2
to Jackson
与杰克逊数据格式 xml https://github.com/FasterXML/jackson-dataformat-xml project
序列化一个Date
对于 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
序列化如下was used:
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
to jackson-dataformat-xml
我知道后者ignores the JAXB2
注释。因此现在与:
@JsonProperty("fecha")
@JsonSerialize(using=JsonDateSerializer.class)
@JacksonXmlProperty(localName="fecha")
@XmlJavaTypeAdapter(XmlDateAdapter.class)
public Date getFecha() {
return fecha;
}
因此从@XmlElement(name="fecha")
to @JacksonXmlProperty(localName="fecha")
现在的问题是@XmlJavaTypeAdapter(XmlDateAdapter.class)
也被忽略。
Question:
等效的注释是什么@XmlJavaTypeAdapter
from JAXB2
to jackson-dataformat-xml