Search code examples
javajaxb2

JAXB : How to manipuulate the Data during Unmarshalling process


I am using jaxb for the Unmarshalling Process

This is my Request

<kiran acct = "1234567" />

package com;
@XmlRootElement(name = "kiran")
@XmlAccessorType(XmlAccessType.FIELD)
public class CustAcct {

    @XmlAttribute
    private String acct;

    public CustAcct() {

        super();
    }

    public String getAcct() {
        System.out.println("The New Getter Method of Account is called");
        return acct;
    }

    public void setAcct(String s) {
        System.out.println("The New Setter Method of Account  is called");
        acct = s;
    }

}

This is the way Jersey (Restful Framework ) automatically binds the data with JAXB

public class HelloService {

    @POST
    @Produces("application/text")
    public String sayPlainTextHello(CustAcct custdata) {

        System.out.println("The New Account is " + custdata.getAcct());
        return "Hi";
    }

}

Here my question is that , why the Setter Method setAcct is not getting called in this case ??

I mean why the Line "The New Setter Method of Account is called" isn't being printed , and where as the Line inside the getMethod is geting called (The New Getter Method of Account is called)

Basically I want to Manupulate Data to an Attribute which is sent in the Request XML .

is there any call back method by which i can control the way Data is being Set ??

Thanks in advance .


Solution

  • why the Setter Method setAcct is not getting called in this case ??

    Because you set the access type to field: @XmlAccessorType(XmlAccessType.FIELD). Change it to @XmlAccessorType(XmlAccessType.PROPERTY)

    XmlAccessType javadoc.

    is there any call back method by which i can control the way Data is being Set ??

    Yes. You have complete control on the marshall/unmarshall process when you use adapters.