3

我有一个使用该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转移JABX2Jackson使用的应用程序

要序列化一个Datefor 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)被忽略了。

问题

@XmlJavaTypeAdapterfrom JAXB2to的等价注解是什么jackson-dataformat-xml

4

0 回答 0