Aggiunto l'esempio di JAXB e JSON-P
This commit is contained in:
71
xmlandjsondemo/pom.xml
Normal file
71
xmlandjsondemo/pom.xml
Normal file
@@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>xmlandjsondemo</artifactId>
|
||||
<name>xmlandjsondemo</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>jeedemo</artifactId>
|
||||
<groupId>it.plague.jeedemo</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.json</groupId>
|
||||
<artifactId>javax.json-api</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.12.4</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>integration-test</id>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,26 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@XmlRootElement
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class CreditCard {
|
||||
|
||||
@XmlAttribute
|
||||
private String number;
|
||||
@XmlElement(name = "expiry_date")
|
||||
private String expiryDate;
|
||||
@XmlElement(name = "control_number")
|
||||
private Integer controlNumber;
|
||||
private String type;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import javax.json.Json;
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CreditCardJSonTest {
|
||||
|
||||
public static final String creditCardJSon =
|
||||
"{\"creditCard\":" +
|
||||
"{\"number\":\"12345678\"," +
|
||||
"\"expiryDate\":\"10/14\"," +
|
||||
"\"controlNumber\":566," +
|
||||
"\"type\":\"Visa\"}" +
|
||||
"}";
|
||||
|
||||
@Test
|
||||
public void shouldGenerateACreditCard() {
|
||||
CreditCard creditCard = new CreditCard("12345678", "10/14", 566, "Visa");
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonGenerator generator = Json.createGenerator(writer);
|
||||
generator.writeStartObject()
|
||||
.writeStartObject("creditCard")
|
||||
.write("number", creditCard.getNumber())
|
||||
.write("expiryDate", creditCard.getExpiryDate())
|
||||
.write("controlNumber", creditCard.getControlNumber())
|
||||
.write("type", creditCard.getType())
|
||||
.writeEnd()
|
||||
.writeEnd()
|
||||
.close();
|
||||
assertEquals(creditCardJSon, writer.toString().trim());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CreditCardXmlTest {
|
||||
|
||||
public static final String creditCardXml =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
|
||||
"<creditCard number=\"12345678\">" +
|
||||
"<expiry_date>10/14</expiry_date>" +
|
||||
"<control_number>566</control_number>" +
|
||||
"<type>Visa</type>" +
|
||||
"</creditCard>";
|
||||
|
||||
@Test
|
||||
public void shouldMarshallACreditCard() throws JAXBException {
|
||||
CreditCard creditCard = new CreditCard("12345678", "10/14", 566, "Visa");
|
||||
StringWriter writer = new StringWriter();
|
||||
JAXBContext context = JAXBContext.newInstance(CreditCard.class);
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
marshaller.marshal(creditCard, writer);
|
||||
System.out.println(writer);
|
||||
assertEquals(creditCardXml, writer.toString().trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUnmarshallACreditCard() throws JAXBException {
|
||||
StringReader reader = new StringReader(creditCardXml);
|
||||
JAXBContext context = JAXBContext.newInstance(CreditCard.class);
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
CreditCard creditCard = (CreditCard) unmarshaller.unmarshal(reader);
|
||||
assertEquals("12345678", creditCard.getNumber());
|
||||
assertEquals("10/14", creditCard.getExpiryDate());
|
||||
assertEquals((Object) 566, creditCard.getControlNumber());
|
||||
assertEquals("Visa", creditCard.getType());
|
||||
}
|
||||
|
||||
}
|
||||
2
xmlandjsondemo/xmlandjsondemo.iml
Normal file
2
xmlandjsondemo/xmlandjsondemo.iml
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
||||
Reference in New Issue
Block a user