The problem of passing collection parameters between SP and SC when using xfire+JAXB

来源:百度文库 编辑:神马文学网 时间:2024/07/03 08:25:24
The reason of failing to unmashall the xml to java object at SC side is the element names are different
between the SP and SC.
The response given by SP is


alex.yuan@hp.com
alex
10


linda@hp.com
linda
9


The embedded element should be . This is just what the SC anticipates.
The signature of the method SP provides is
@WebMethod
public @WebResult(name="getMembersResponse")
Collection getMembers();
Unfortunately, the class MemberType is mapped to an element whose name is "memberType"
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "memberType", propOrder = {
"name",
"mail",
"rank"
})
public class MemberType {
These POJOs are generated by XJC from JAXB2. I guess this problem relates to XJC?
Yes. It relates to XJC. For a schema as follows, XJC will only generate CustomerType.java but not Customer.java. This is INCORRECT. This gives me a wrong impression that the service should uses instance of CustomerType to fill the returning collection.
When using Castor, it will generate two java files, CustomerType and Customer. Instances of Customer should be used to fill the returned collection.
==============================================================================================
New Update :
Yes. This relates to JAXB but not a wrong behavior.  The difference in design between JAXB and Castor caused the misunderstanding.
I am new to JAXB. What I know now is JAXB heavily uses JAXBElement, a generic class, to express an element within an XML document.  XJC generates java classes for every type it finds in schema but not for elements in schema. It is expected to use JAXBElement to express these elements.
Meanwhile, XJC generates an auxiliary class called ObjectFactory whose purpose is to aim you to create the POJOs expressing data in XML.
So, in short, the example should be modified as follows.
The signature of the method SP provides is
@WebMethod
public @WebResult(name="getMembersResponse")
GetMembersResponseType getMembers();
GetMembersResponseType is a generated class.
The service implementation class should look like,
public class MemberServiceImpl implements MemberService {
ArrayList members;
...
public GetMembersResponseType getMembers() {
GetMembersResponseType getmembersResponse = factory.createGetMembersResponseType();
ArrayOfMemberType arrayOfMemberType = factory.createArrayOfMemberType();
arrayOfMemberType.member = members;
getmembersResponse.members = arrayOfMemberType;
return getmembersResponse;
}