Merge changes from branch
This commit is contained in:
parent
66e6ae6c19
commit
8442fc354b
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<book id="1">
|
||||||
|
<title>Book1</title>
|
||||||
|
<date>2016-12-16T17:28:49.718Z</date>
|
||||||
|
</book>
|
27
jaxb/pom.xml
27
jaxb/pom.xml
|
@ -23,7 +23,13 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.sun.istack</groupId>
|
<groupId>com.sun.istack</groupId>
|
||||||
<artifactId>istack-commons-runtime</artifactId>
|
<artifactId>istack-commons-runtime</artifactId>
|
||||||
<version>${istack.version}</version>
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -44,6 +50,17 @@
|
||||||
<version>${logback.version}</version>
|
<version>${logback.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.5</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -60,7 +77,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.eclipse.m2e</groupId>
|
<groupId>org.eclipse.m2e</groupId>
|
||||||
<artifactId>lifecycle-mapping</artifactId>
|
<artifactId>lifecycle-mapping</artifactId>
|
||||||
<version>${lifecycle-mapping.version}</version>
|
<version>1.0.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<lifecycleMappingMetadata>
|
<lifecycleMappingMetadata>
|
||||||
<pluginExecutions>
|
<pluginExecutions>
|
||||||
|
@ -158,16 +175,14 @@
|
||||||
<!-- jaxb -->
|
<!-- jaxb -->
|
||||||
<jaxb.version>2.2.11</jaxb.version>
|
<jaxb.version>2.2.11</jaxb.version>
|
||||||
|
|
||||||
<istack.version>3.0.2</istack.version>
|
|
||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||||
<logback.version>1.1.7</logback.version>
|
<logback.version>1.1.7</logback.version>
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
|
||||||
<jaxb2-maven-plugin.version>2.3</jaxb2-maven-plugin.version>
|
<jaxb2-maven-plugin.version>2.3</jaxb2-maven-plugin.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<book id="1">
|
||||||
|
<title>Book1</title>
|
||||||
|
<date>2016-12-16T17:28:49.718Z</date>
|
||||||
|
</book>
|
|
@ -8,6 +8,10 @@ import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import javax.xml.bind.annotation.XmlTransient;
|
import javax.xml.bind.annotation.XmlTransient;
|
||||||
import javax.xml.bind.annotation.XmlType;
|
import javax.xml.bind.annotation.XmlType;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.EqualsBuilder;
|
||||||
|
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
||||||
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
|
|
||||||
@XmlRootElement(name = "book")
|
@XmlRootElement(name = "book")
|
||||||
@XmlType(propOrder = { "id", "name", "date" })
|
@XmlType(propOrder = { "id", "name", "date" })
|
||||||
public class Book {
|
public class Book {
|
||||||
|
@ -53,6 +57,17 @@ public class Book {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", date=" + date + "]";
|
return ToStringBuilder.reflectionToString(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return EqualsBuilder.reflectionEquals(this, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return HashCodeBuilder.reflectionHashCode(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
|
@ -16,7 +17,8 @@ public class Main {
|
||||||
book.setId(1L);
|
book.setId(1L);
|
||||||
book.setName("Book1");
|
book.setName("Book1");
|
||||||
book.setAuthor("Author1");
|
book.setAuthor("Author1");
|
||||||
book.setDate(new Date());
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
|
book.setDate(new Date(1481909329718L));
|
||||||
|
|
||||||
JAXBContext context = JAXBContext.newInstance(Book.class);
|
JAXBContext context = JAXBContext.newInstance(Book.class);
|
||||||
Marshaller marshaller = context.createMarshaller();
|
Marshaller marshaller = context.createMarshaller();
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.jaxb.test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Marshaller;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
|
|
||||||
|
import com.baeldung.jaxb.Book;
|
||||||
|
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
|
public class JaxbTest {
|
||||||
|
Book book;
|
||||||
|
JAXBContext context;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() throws JAXBException {
|
||||||
|
book = new Book();
|
||||||
|
book.setId(1L);
|
||||||
|
book.setName("Book1");
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
|
book.setDate(new Date(1481909329718L));
|
||||||
|
context = JAXBContext.newInstance(Book.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void marshal() throws JAXBException, IOException {
|
||||||
|
Marshaller marshaller = context.createMarshaller();
|
||||||
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||||
|
marshaller.marshal(book, new File("./book.xml"));
|
||||||
|
String sampleBookXML = FileUtils.readFileToString(new File("./sample_book.xml"), "UTF-8");
|
||||||
|
String marshallerBookXML = FileUtils.readFileToString(new File("./book.xml"), "UTF-8");
|
||||||
|
Assert.assertEquals(sampleBookXML, marshallerBookXML);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void unMashal() throws JAXBException, IOException {
|
||||||
|
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||||
|
Book unMarshallerbook = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
|
||||||
|
Assert.assertEquals(book, unMarshallerbook);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue