JAXBElement.getValue () returns null

I have one to many mapping in my Pojo classes. The store has a branch and the branch has many stores Here is the store code:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Shop")
public class Shop {

@XmlID
private String name;
@XmlIDREF
@XmlElement(name="ShopBranch",type=Branch.class)
private Branch branch;
//Getter Setter
}

      

Below is the code for the branch:

 @XmlAccessorType(XmlAccessType.FIELD)
public class Branch {
@XmlID
private String name;
private String address;
@XmlIDREF
@XmlElement(nillable=false,required=true)
private List<Shop> shops;
//Getter and Setters
}

      

I am posting a webservice with some basic methods. and my wsimport generates a Branch class as shown below

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "branch", propOrder = {
    "branchName",
    "address",
    "branchShop"
})
public class Branch {

    @XmlElement(name = "BranchName")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String branchName;
    protected String address;
    @XmlElementRef(name = "BranchShop", type = JAXBElement.class)
    protected List<JAXBElement<Object>> branchShop;
    //Getter-Setter
    }

      

I don't know why it is List <JAXBElement <Object →> and not List <JAXBElement <Shop>. But anyway. I have a method that returns all branches and this works fine. When I checkout the branchShop from the branch instance, I get the correct size for the branchShop list, but NULL is returned for all items in the getValue list. Below is the short code:

PencilCatalog service= new PencilCatalog();
com.pencilhouse.webservices.PencilService port=service.getPencilCatalogPort();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, Constant.PENCIL_SERVICE);
    List<Branch> branches= port.getAllBranches();
for(Branch b:branches)
{
    System.out.println("******************Branch:"+b.getBranchName()+" "+b.getAddress()+"******************");
    JAXBElement<Object>o=b.getBranchShop().get(0);
    System.out.println(o+"Value"+o.getScope()+" "+o.getValue());
}

      

o / r

****************** Industry: KukatPalli Steer 2 Kukatpalli ****************** javax.xml.bind.JAXBElement @ 45d9d7beValueclass com.pencilhouse.webservices.Branch null

The generated WSDL is pretty big. I only post the branch and store type. I am publishing a web service using Endpoint

Generated XML:

<xs:complexType name="shop">
<xs:sequence>
<xs:element name="name" type="xs:ID" minOccurs="0"/>
<xs:element name="ShopBranch" type="xs:IDREF" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="branch">
<xs:sequence>
<xs:element name="BranchName" type="xs:ID" minOccurs="0"/>
<xs:element name="address" type="xs:string" minOccurs="0"/>
<xs:element name="BranchShop" type="xs:IDREF" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

      

Intercepted information: Request:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getAllBranches xmlns:ns2="PencilServiceHouse"/>
</S:Body>
</S:Envelope>

      

Answer:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getAllBranchesResponse xmlns:ns2="PencilServiceHouse">
        <return>
            <name>
                    KukatPalli
            </name>
            <address>
                    Steer 2 Kukatpalli
            </address>
            <shops>
                    Pencil World    <!-- This is Shop Information which is coming as NULL in java, This is Shop Name field which is declared as id using @XmlId -->
            </shops>
            <shops>
                    Pencils Den
            </shops>
            <shops>
                    Pencils Bag
            </shops>
        </return>
        <return>
            <name>
                    Salt Lake
            </name>
            <address>
                    Sec V Salt Lake
            </address>
            <shops>
                    Pencil World
            </shops>
            <shops>
                    Pencils Den
            </shops>
        </return>
        <return>
            <name>
                    Noida
            </name>
            <address>
                    Noida Sec 43
            </address>
            <shops>
                    Pencils Bag
            </shops>
        </return>
        </ns2:getAllBranchesResponse>
    </S:Body>
</S:Envelope>

      

+3


source to share


2 answers


@XmlIDREF

provides a way to specify links within a document. Each object is required to be reset via a separated nested relationship (for example @XmlElement

) in order to retrieve the data in the XML document.

I wrote more about @XmlIDREF

on my blog:


UPDATE



I saw your blog, I am getting data in my xml answer, but the problem is that when I create my classes on the client side using wsimport, it is a generating type object for @XmlIDRef and it does not set the data for these as I explained in my question.

In your example, you are going: classes -> schema -> classes. Because Java classes and XML Schema do not match, JAXB does not put any JAXB specific metadata in the generated schema, some information is lost. You can fix it like this:

You can use an external bindings file to inject the IDREF property.

<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
    <jaxb:bindings node="xsd:complexType[@name='branch']//xsd:element[@name='BranchShop']">
        <jaxb:property>
            <jaxb:baseType name="org.example.Shop"/>
        </jaxb:property>
    </jaxb:bindings>
</jaxb:bindings>

      

0


source


This is not an answer, but it is big for a comment and uses some formatting, so I have to post it as an answer.


Somehow your examples just don't add up.



First of all, it is suspicious that you have two classes Branch

. Which already looks strange. Why do you need a second one? Or is it, for example, services and client-side implementations?

Then your classes Branch

use the BranchName

and elements BranchShop

, whereas your XML uses the name

and elements shop

. This does not fit. It looks like your webservice is not giving the expected XML. This explains why the data is missing, but what puzzles me is why you are getting the item at all BranchShop

. I would actually expect the list to be empty, but it looks like you have something.

My suggestion was to figure out why the XML you are getting doesn't match your schema and implement an isolated unmarshalling unit-test to test the unmarshalling of the intercepted XML.

0


source







All Articles