Created
October 30, 2012 15:18
-
-
Save liweinan/3980877 to your computer and use it in GitHub Desktop.
RESTEasy Jackson+JAXB Investigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.bluedash.resteasy.jackson; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.Marshaller; | |
/** | |
* 10 30 2012 | |
* | |
* @author <a href="mailto:l.weinan@gmail.com">Weinan Li</a> | |
*/ | |
public class TestXmlResource { | |
public static void main(String[] args) throws Exception { | |
JAXBContext jaxbContext = JAXBContext.newInstance(XmlResource.class); | |
XmlResource resource = new XmlResource(); | |
resource.setAttr1("XXX"); | |
resource.setAttr2("YYY"); | |
Marshaller marshaller = jaxbContext.createMarshaller(); | |
System.out.println("# JAXB:"); | |
marshaller.marshal(resource, System.out); | |
ObjectMapper mapper = new ObjectMapper(); | |
System.out.println("\n\n# JACKSON:"); | |
mapper.writeValue(System.out, resource); | |
} | |
} | |
--- | |
package net.bluedash.resteasy.jackson; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
/** | |
* 10 30 2012 | |
* | |
* @author <a href="mailto:l.weinan@gmail.com">Weinan Li</a> | |
*/ | |
@XmlRootElement | |
public class XmlResource { | |
String attr1; | |
String attr2; | |
@XmlElement(name = "attr_1") | |
public String getAttr1() { | |
return attr1; | |
} | |
public void setAttr1(String attr1) { | |
this.attr1 = attr1; | |
} | |
@XmlElement | |
public String getAttr2() { | |
return attr2; | |
} | |
public void setAttr2(String attr2) { | |
this.attr2 = attr2; | |
} | |
} | |
--- | |
# Output: | |
# JAXB: | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xmlResource><attr_1>XXX</attr_1><attr2>YYY</attr2></xmlResource> | |
# JACKSON: | |
{"attr1":"XXX","attr2":"YYY"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment