tag:blogger.com,1999:blog-1710464868724994296.post990954744476582556..comments2023-06-29T09:48:09.653-04:00Comments on Java XML and JSON Binding: JAXB and Immutable ObjectsBlaise Doughanhttp://www.blogger.com/profile/02016807982653343777noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-1710464868724994296.post-57979659192427391312013-02-11T15:06:55.900-05:002013-02-11T15:06:55.900-05:00link worked for me, and I think its the cleanest s...link worked for me, and I think its the cleanest solution by far.. so here is Customer (Java 1.5): for posterity<br /><br /><br />package blog.immutable;<br /><br />import javax.xml.bind.annotation.XmlAccessType;<br />import javax.xml.bind.annotation.XmlAccessorType;<br />import javax.xml.bind.annotation.XmlAttribute;<br />import javax.xml.bind.annotation.XmlElement;<br />import javax.xml.bind.annotation.XmlRootElement;<br /><br />@XmlRootElement(name="customer")<br />@XmlAccessorType(XmlAccessType.NONE)<br />public final class Customer {<br /> <br /> @XmlAttribute<br /> private final String name;<br /> <br /> @XmlElement<br /> private final Address address;<br /> <br /> @SuppressWarnings("unused")<br /> private Customer() {<br /> this(null, null);<br /> }<br /> <br /> public Customer(String name, Address address) {<br /> this.name = name;<br /> this.address = address;<br /> }<br /> <br /> public String getName() {<br /> return name;<br /> }<br /> <br /> public Address getAddress() {<br /> return address;<br /> }<br /> <br />}Jinnhttps://www.blogger.com/profile/12856558469419903795noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-42631109374060516052012-12-12T15:15:29.901-05:002012-12-12T15:15:29.901-05:00Hi Gabriele,
Since Code is an interface depending...Hi Gabriele,<br /><br />Since <i>Code</i> is an interface depending on your JAXB implementation you may need to specify an <i>@XmlElement(type=CodeImpl.class)</i> where <i>CodeImpl</i> is a JAXB friendly object and they write an XmlAdapter that converts to/from that to your immutable object.<br />- <a href="http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html" rel="nofollow">JAXB and Interface Fronted Models</a><br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-18090198908564930442012-12-11T12:00:43.214-05:002012-12-11T12:00:43.214-05:00Hi,
I'm trying to map a collection of immutab...Hi,<br /><br />I'm trying to map a collection of immutable objects by using interface on elements, i.e. the collection property type is List< Code> with Code an interface. <br /><br />Should I create the Adapter so that it adapts to/from the interface or the implementation?<br /><br />I tried both ways and JAXB RI complains that it can't find a no-arg contructor, as if it was ignoring the XmlJavaTypeAdapter.<br /><br />Is what I'm trying to do a supported scenario?<br /><br />Thanks,<br />GabrieleGabrielenoreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-42546093448678299052012-03-14T14:27:00.135-04:002012-03-14T14:27:00.135-04:00Man I wish this was accessible...Man I wish this was accessible...James E. Ervin, IVhttps://www.blogger.com/profile/08449037734048661986noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-60420601064019647802011-05-05T14:30:54.649-04:002011-05-05T14:30:54.649-04:00Hi John,
EclipseLink JAXB (MOXy) offers the abili...Hi John,<br /><br /><a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow">EclipseLink JAXB (MOXy)</a> offers the ability to specify the metadata as an XML file:<br /><br />- <a href="http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html" rel="nofollow">Extending JAXB - Representing Metadata as XML</a><br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-14241386864568557552011-05-04T17:22:29.712-04:002011-05-04T17:22:29.712-04:00Is there a way to push the
@XmlJavaTypeAdapter(C...Is there a way to push the <br /><br />@XmlJavaTypeAdapter(CustomerAdapter.class)<br /><br />Into config rather than annotations? My domain model is in a third part lib.<br /><br />JDJohnhttps://www.blogger.com/profile/09118843984388626400noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-6890287409761532372011-02-25T15:57:47.315-05:002011-02-25T15:57:47.315-05:00Hi Julio,
I wouldn't use XmlAdapter for this ...Hi Julio,<br /><br />I wouldn't use XmlAdapter for this scenario. Instead you could mark the payload property with @XmlAnyElement. Then you could do one of the following:<br /><br /><b>1. @XmlAnyElement(lax=true)</b><br /><br />You could have one model for the message envelope, and other models for the individual payloads. My post below gives an example of how this could be done.<br /><br />- <a href="http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html" rel="nofollow">Using @XmlAnyElement to Build a Generic Message</a><br /><br /><b>2. @XmlAnyElement</b><br /><br />If you just mark the payload property with @XmlAnyElement the contents will be kept as a DOM node (this should still work if you're using JAXB to process JSON via something like Jettison). Then later you can unmarshal this DOM node into the correct payload.<br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-10484138717026768452011-02-25T15:21:37.027-05:002011-02-25T15:21:37.027-05:00Hi -
So we have an interesting problem and I'...Hi - <br /><br />So we have an interesting problem and I'm not sure if XmlAdapter can be used. Basically, we have this scencario: <br /><br />We want to use JAXB to annotate POJO classes for marshalling/unmarshalling into JSON objects, instead of XML. The format of the JSON object is a simple event message as follows:<br /> - eventTime: A JSON timestamp<br /> - eventType: A JSON String denoting the event type<br /> - payload: A generic JSON object<br /><br />So the problem is we want to keep the "payload" property of the message opaque for as long as possible. Hence, we would like to parse the JSON message object into a POJO like this:<br /><br />public class EventMessage {<br /> public Date eventTime;<br /> public String eventType;<br /><br /> // A Java Object, or some generic JAXB equivalent <br /> public Object payload;<br />}<br /><br />Then down the line some bit of logic can examine the eventType, determine that it's interested in the message, and do a second pass to parse the "payload" property into something concrete (i.e., if this was a user update message, then maybe into a POJO called UserUpdatePayload).<br /><br />Thanks for your help,<br />-JulioJulio M.noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-5840694223805719312011-01-26T03:39:02.997-05:002011-01-26T03:39:02.997-05:00Thanks for this post, it was very informative.
As...Thanks for this post, it was very informative.<br /><br />As part of the research we've been doing on our project, we discovered that since Java 1.5 (and thanks to JSR-133) it's possible to construct objects using reflection that have only a private constructor as well as only final fields, and JAXB can take advantage of this.<br /><br />For those that aren't a fan of having to essentially create a copy of the Customer object (as in the above example) you can define a private no-arg constructor as follows:<br /><br />private Customer() {<br /> this.name = null;<br /> this.address = null;<br />}<br /><br />Then you don't actually need to create a type adapter. I posted a working example of this based on your post at the following location:<br /><br />https://test.kuali.org/svn/rice/sandbox/immutable-jaxb/Eric Westfallhttps://www.blogger.com/profile/06459409744281063525noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-45528254762812791402011-01-21T15:46:12.410-05:002011-01-21T15:46:12.410-05:00Hi Oleksiy,
The root object case is not quite as ...Hi Oleksiy,<br /><br />The root object case is not quite as clean. The result of the unmarshal operation will be the adapted object, then you will need to call the conversion code yourself.<br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-13761520195731788492011-01-16T17:18:55.918-05:002011-01-16T17:18:55.918-05:00Hi Blaise
I've tried to use XmlAdapter to bin...Hi Blaise<br /><br />I've tried to use XmlAdapter to bind my immutable object, and there is a problem i've faced.<br /><br />In your example PurchaseOrder instance is mutable, and its member is immutable. I've tried and it works that way fine.<br /><br />In my case i need the root object to be immutable. So I tried to use @XmlJavaTypeAdapter with @XmlRootElement and it didn't worked.<br /><br />So I'm wondering if that is correct behavior and is there a way to use XmlAdapter for root elements?<br /><br />- Oleksiy0x1ehttps://www.blogger.com/profile/13780658737498694489noreply@blogger.com