Hi,
I just did a test to see how JAXB-RI handles the POJO without any
annotations. The result seems to be promising.
I started with a POJO:
public class MyBean {
private int age;
private String name;
private List<String> notes = new ArrayList<String>();
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getNotes() {
return notes;
}
public void setNotes(List<String> notes) {
this.notes = notes;
}
}
The following test case is then successful.
public void testPOJO() throws Exception {
JAXBContext context = JAXBContext.newInstance(MyBean.class);
StringWriter writer = new StringWriter();
MyBean bean = new MyBean();
bean.setName("Test");
bean.setAge(20);
bean.getNotes().add("1");
bean.getNotes().add("2");
JAXBElement<Object> element = new JAXBElement<Object>(new
QName("http://ns1", "bean"), Object.class, bean);
context.createMarshaller().marshal(element, writer);
System.out.println(writer.toString());
Object result = context.createUnmarshaller().unmarshal(new
StringReader(writer.toString()));
assertTrue(result instanceof JAXBElement);
JAXBElement e2 = (JAXBElement)result;
assertTrue(e2.getValue() instanceof MyBean);
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:bean
xsi:type="myBean" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://ns1"><age>20</age><name>Test</name></ns2:bean>
Thanks,
Raymond
----- Original Message -----
From: "Simon Nash" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Monday, November 26, 2007 1:50 PM
Subject: Re: Data transformation from/to POJO
There is a default Java to XML mapping (without annotations) in the
JAXB spec, in addition to the customized mapping (with annotations).
Does the default mapping have unacceptable limitations? If so, what
are they?
Simon
Raymond Feng wrote:
Hi,
I'm on the same boat as Mike and you. The discussion was about how can we
simplify the data transformation of a subset of POJOs following a strict
pattern without starting from a formal model such as XSD. I don't know
any JAXB implementation can handle a POJO without JAXB annotations. If
there is one with reasonable support of default Java/XML mapping (no XSD
or annotations are required), I would be happy to use it.
Thanks,
Raymond
----- Original Message ----- From: "Simon Nash" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Monday, November 26, 2007 12:36 PM
Subject: Re: Data transformation from/to POJO
Mike has brought up a very good point. I don't think it would make
sense for Tuscany to invent yet another Java to XML mapping. What
are the issues if we were to go with what JAXB defines for this?
Simon
Mike Edwards wrote:
Raymond,
"Where angels fear to tread"
My initial thoughts about this mused on why people had spent so much
time on specs like SDO and JAXB. If mapping POJOs to XML was simple
and straightforward, why did we need those large specs?
Perhaps you are right in thinking that there are simple cases that can
be mapped simply. But then, what do you do about the more awkward
cases?
What I'd like us to consider deeply first is whether we want to create
(yet) another Java <-> XML mapping specification and if so, what is its
relationship to the existing ones.
My initial 2 cents....
Yours, Mike.
Raymond Feng wrote:
Hi,
With the recent development of the online store tutorial, we encounter
quite a few issues around the transformation between POJO and other
databindings (such as XML, JSON).
Let's take the POJO <--> XML as an example. Here is a set of questions
to be answered.
1) Do we require the POJO to be a strict JavaBean or free-form class?
2) How to read properties from a java object?
The data in a java object can be accessed by the field or by JavaBean
style getter methods. There are different strategies:
a) Always use JavaBean-style getter method
b) Always use field access
c) A combination of a & b
The other factor is the modifier of a field/method defintion. What
modifiers are allowed? public, protected, default and private?
If a property only have getter method, should we dump the property
into the XML? How about transient fields?
3) How to write properties to populate the target POJO instance?
a) Use JavaBean setter?
b) Use field
c) Combination of a & b
When we convert XML element back to a POJO property, how do we
instantiate the property instance if the property type is an interface
or abstract class?
For example,
package com.example;
public class MyBean {
private MyInterface p1;
public void setP1(MyInterface p1) {
this.p1 = p1;
}
public MyInterface getP1() {
return p1;
}
}
Do we require the XML element contains xsi:type attribute which will
be generated from POJO-->XML to represent the concrete property type?
Such as:
<myBean xsi:type="ns1:MyBean" xmlns:ns1="http://example.com/">
<p1 xsi:type="ns2:MyInterface" xmlns:ns2=http://example.com//>
</myBean>
Thanks,
Raymond
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]