unmarshalling dates using JAXB - examples (#7672)

This commit is contained in:
Catalin Burcea 2019-09-14 12:46:52 +03:00 committed by ashleyfrieze
parent da781bf1d1
commit 2a165908c2
9 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.baeldung.jaxb.dateunmarshalling;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.datatype.XMLGregorianCalendar;
@XmlRootElement(name = "book")
public class Book {
@XmlElement(name = "title", required = true)
private String title;
@XmlElement(name = "published", required = true)
private XMLGregorianCalendar published;
public XMLGregorianCalendar getPublished() {
return published;
}
@Override
public String toString() {
return "[title: " + title + "; published: " + published.toString() + "]";
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.jaxb.dateunmarshalling;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;
@XmlRootElement(name = "book")
public class BookDateAdapter {
@XmlElement(name = "title", required = true)
private String title;
@XmlElement(name = "published", required = true)
@XmlJavaTypeAdapter(DateAdapter.class)
private Date published;
public Date getPublished() {
return published;
}
@Override
public String toString() {
return "[title: " + title + "; published: " + published.toString() + "]";
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.jaxb.dateunmarshalling;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.LocalDateTime;
@XmlRootElement(name = "book")
public class BookLocalDateTimeAdapter {
@XmlElement(name = "title", required = true)
private String title;
@XmlElement(name = "published", required = true)
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
private LocalDateTime published;
public LocalDateTime getPublished() {
return published;
}
@Override
public String toString() {
return "[title: " + title + "; published: " + published.toString() + "]";
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.jaxb.dateunmarshalling;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateAdapter extends XmlAdapter<String, Date> {
private static final String CUSTOM_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
@Override
public String marshal(Date v) {
return new SimpleDateFormat(CUSTOM_FORMAT_STRING).format(v);
}
@Override
public Date unmarshal(String v) throws Exception {
return new SimpleDateFormat(CUSTOM_FORMAT_STRING).parse(v);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.jaxb.dateunmarshalling;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
public class JaxbDateUnmarshalling {
public static final String DEFAULT_DATE_UNMARSHALLING_FILE = "default-date-unmarshalling.xml";
public static final String CUSTOM_DATE_UNMARSHALLING_FILE = "custom-date-unmarshalling.xml";
public static Book unmarshalDates(InputStream inputFile) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (Book) jaxbUnmarshaller.unmarshal(inputFile);
}
public static BookDateAdapter unmarshalDatesUsingCustomXmlAdapter(InputStream inputFile) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(BookDateAdapter.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (BookDateAdapter) jaxbUnmarshaller.unmarshal(inputFile);
}
public static BookLocalDateTimeAdapter unmarshalDatesUsingJava8(InputStream inputFile) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(BookLocalDateTimeAdapter.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (BookLocalDateTimeAdapter) jaxbUnmarshaller.unmarshal(inputFile);
}
public static InputStream getInputStream(String file) {
ClassLoader classLoader = JaxbDateUnmarshalling.class.getClassLoader();
return classLoader.getResourceAsStream(file);
}
public static void main(String[] args) throws JAXBException {
Book book = unmarshalDates(getInputStream(DEFAULT_DATE_UNMARSHALLING_FILE));
BookDateAdapter bookDateAdapter = unmarshalDatesUsingCustomXmlAdapter(getInputStream(CUSTOM_DATE_UNMARSHALLING_FILE));
BookLocalDateTimeAdapter bookLocalDateTimeAdapter = unmarshalDatesUsingJava8(getInputStream(CUSTOM_DATE_UNMARSHALLING_FILE));
System.out.println(book);
System.out.println(bookDateAdapter);
System.out.println(bookLocalDateTimeAdapter);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.jaxb.dateunmarshalling;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
private DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public String marshal(LocalDateTime dateTime) {
return dateTime.format(dateFormat);
}
@Override
public LocalDateTime unmarshal(String dateTime) {
return LocalDateTime.parse(dateTime, dateFormat);
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>Book1</title>
<published>1979-11-28 02:31:32</published>
</book>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>Book1</title>
<published>1979-11-28T02:31:32</published>
</book>

View File

@ -0,0 +1,52 @@
package com.baeldung.jaxb.dateunmarshalling;
import org.junit.Test;
import javax.xml.bind.JAXBException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import static org.junit.Assert.assertEquals;
public class JaxbDateUnmarshallingUnitTest {
@Test
public void whenUnmarshalDatesIsCalled_ThenCorrectDateIsReturned() throws JAXBException, DatatypeConfigurationException {
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.DEFAULT_DATE_UNMARSHALLING_FILE);
XMLGregorianCalendar expected = DatatypeFactory.newInstance().newXMLGregorianCalendar("1979-11-28T02:31:32");
Book book = JaxbDateUnmarshalling.unmarshalDates(inputStream);
assertEquals(expected, book.getPublished());
}
@Test
public void whenUnmarshalDatesUsingCustomXmlAdapterIsCalled_ThenCorrectDateIsReturned() throws JAXBException, ParseException {
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date expected = format.parse("1979-11-28 02:31:32");
BookDateAdapter book = JaxbDateUnmarshalling.unmarshalDatesUsingCustomXmlAdapter(inputStream);
assertEquals(expected, book.getPublished());
}
@Test
public void whenUnmarshalDatesUsingJava8IsCalled_ThenCorrectDateIsReturned() throws JAXBException {
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE);
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime expected = LocalDateTime.parse("1979-11-28 02:31:32", dateFormat);
BookLocalDateTimeAdapter book = JaxbDateUnmarshalling.unmarshalDatesUsingJava8(inputStream);
assertEquals(expected, book.getPublished());
}
}