Spring Hot(1)Jersey Integration
1. Marshal and UnMarshal the Object to XML
The object with annotation
package com.sillycat.easyspringrest.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Product {
private Long id;
private String productName;
private String productLink;
private String desn;
//XmlTransient means that we will ignore in generated XML
@XmlTransient
public Long getId() {
returnid;
}
publicvoid setId(Long id) {
this.id = id;
}
@XmlElement(name="name")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductLink() {
return productLink;
}
public void setProductLink(String productLink) {
this.productLink = productLink;
}
public String getDesn() {
return desn;
}
public void setDesn(String desn) {
this.desn = desn;
}
@Override
public String toString() {
return"Product [id=" + id + ", productName=" + productName + ", productLink=" + productLink + ", desn=" + desn + "]";
}
}
The main Application to Marshal and Unmarshal the Object
package com.sillycat.easyspringrest.model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ProductJabxMain {
public static void main(String[] args) {
Product item1 = new Product();
item1.setDesn("desn");
item1.setId(Long.valueOf(1));
item1.setProductLink("http://github.com/luohuazju");
item1.setProductName("Product Name");
try {
File file = new File("/tmp/product.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(item1, file);
} catch (JAXBException e) {
e.printStackTrace();
}
JAXBContext jaxbContext = null;
Product item2 = null;
FileReader fileReader = null;
try {
fileReader = new FileReader("/tmp/product.xml");
jaxbContext = JAXBContext.newInstance(Product.class);
item2 = (Product) jaxbContext.createUnmarshaller().unmarshal(
fileReader);
System.out.println("Item:" + item2);
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
2. Marshal and Unmarshal via Jackson
Dependency
<dependencyorg="org/codehaus/jackson"name="jackson-core-lgpl"rev="1.9.10"/><dependencyorg="org/codehaus/jackson"name="jackson-mapper-lgpl"rev="1.9.10"/>
Some new annotation
//XmlTransient means that we will ignore in generated XML@XmlTransient@JsonIgnore
@XmlElement(name="name")
@JsonProperty("name")
public String getProductName() {
return productName;
}
The Main Application to Test the Annotation
package com.sillycat.easyspringrest.model;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
public class ProductJSONMain {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);
//convert Object to json string
Product item1 = new Product();
item1.setDesn("desn");
item1.setId(Long.valueOf(1));
item1.setProductLink("http://github.com/luohuazju");
item1.setProductName("Product Name"); //write to file
File file = new File("/tmp/product.json");
objectMapper.writeValue(file, item1); //read json file data to String
byte[] jsonData = Files.readAllBytes(Paths.get("/tmp/product.json"));
//convert json string to object
Product item = objectMapper.readValue(jsonData, Product.class); System.out.println("Product Object\n" + item);
}
}
References:
https://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/
http://www.pigg.co/jersey-spring-rest.html
http://xosadan.iteye.com/blog/1119235
official website
https://jersey.java.net/
https://jersey.java.net/documentation/latest/getting-started.html
Spring MVC Controller REST and Groovy Controller REST
http://sillycat.iteye.com/blog/897285
http://sillycat.iteye.com/blog/897286
http://sillycat.iteye.com/blog/897588
http://sillycat.iteye.com/blog/1477354
http://sillycat.iteye.com/blog/1477387
http://sillycat.iteye.com/blog/1477464
Java XML JSON
http://sillycat.iteye.com/blog/805303 Java Object vs XML
http://docs.oracle.com/javase/tutorial/jaxb/intro/ jaxb doc
https://github.com/FasterXML/jackson-annotations Java Object vs JSON
http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial Jackson Example