jaxb
Nie mogę zmusić JAXB do usunięcia znacznika czasu w aplikacji Resteasy JAX-RS server.
Moja klasa wygląda tak:
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "foo")
public final class Foo {
// Other fields omitted
@XmlElement(name = "timestamp", required = true)
protected Date timestamp;
public Foo() {}
public Date getTimestamp() {
return timestamp;
}
public String getTimestampAsString() {
return (timestamp != null) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp) : null;
}
public void setTimestamp(final Date timestamp) {
this.timestamp = timestamp;
}
public void setTimestamp(final String timestampAsString) {
try {
this.timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestampAsString);
} catch (ParseException ex) {
this.timestamp = null;
}
}
}
Jakieś pomysły?
Dzięki. 4 answers
JAXB radzi sobie z Javą.util.Zajęcia z randkami. Jednak oczekuje formatu:
"yyyy-MM-dd' HH: mm: ss "zamiast" yyyy-MM-dd HH: mm: ss "
Jeśli chcesz użyć tego formatu daty, sugerowałbym użycie Xmladaptera, wyglądałoby to mniej więcej tak:
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DateAdapter extends XmlAdapter<String, Date> {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}
@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.parse(v);
}
}
Następnie należy podać ten adapter we właściwości timestamp:
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "foo")
public final class Foo {
// Other fields omitted
@XmlElement(name = "timestamp", required = true)
@XmlJavaTypeAdapter(DateAdapter.class)
protected Date timestamp;
public Foo() {}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(final Date timestamp) {
this.timestamp = timestamp;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-11 16:31:38
JAXB nie może tworzyć obiektów bezpośrednio, ponieważ nie mają one wystarczającej ilości informacji, aby były jednoznaczne. JAXB przedstawił XmlGregorianCalendar
klasy do tego celu, ale jest to bardzo nieprzyjemne w użyciu bezpośrednio.
Proponuję zmienić twoje pole timestamp
na XmlGregorianCalendar
I zmienić różne metody aktualizacji tego pola, zachowując publiczny interfejs, który już posiadasz, jeśli to możliwe.
Jeśli chcesz zachować pole Date
, musisz zaimplementować własne XmlAdapter
Klasa informująca JAXB jak włączyć Date
do i z XML.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-25 21:16:37
Aby uzyskać XML marshaller do generowania XSD: date sformatowany jako YYYY-MM-DD bez definiowania XmlAdapter użyłem tej metody do zbudowania instancji javax.xml.typ danych.XMLGregorianCalendar:
public XMLGregorianCalendar buildXmlDate(Date date) throws DatatypeConfigurationException {
return date==null ? null : DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd").format(date));
}
Wynikiem zainicjowałem pole xmlgregoriancalendar klasy generowanej przez kompilator JAXB (w Eclipse):
Date now = new Date();
...
report.setMYDATE(buildXmlDateTime(now));
...
JAXBContext context = JAXBContext.newInstance(ReportType.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(new ObjectFactory().createREPORT(report), writer);
I uzyskał znacznik sformatowany zgodnie z oczekiwaniami:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<REPORT>
...
<MY_DATE>2014-04-30</MY_DATE>
...
</REPORT>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-30 08:53:21
Używanie tego adaptera powinno być bezpieczne dla wątku:
public class DateXmlAdapter extends XmlAdapter<String, Date> {
/**
* Thread safe {@link DateFormat}.
*/
private static final ThreadLocal<DateFormat> DATE_FORMAT_TL = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
}
};
@Override
public Date unmarshal(String v) throws Exception {
return DATE_FORMAT_TL.get().parse(v);
}
@Override
public String marshal(Date v) throws Exception {
return DATE_FORMAT_TL.get().format(v);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-13 13:05:30