Re: [jackson-user] I have an overloaded setter on an object, how can I tell Jackson to ignore one of them?

1,143 views
Skip to first unread message

Tatu Saloranta

unread,
Jun 12, 2014, 4:24:46 PM6/12/14
to us...@jackson.codehaus.org, jackso...@googlegroups.com
I think that the problem is due to "cleaved" properties, and you will need to explicitly add @JsonProperty into setter you do want to retain, like so:

        @Override
        public void setData(byte[] data) {
            this.data = data;
        }

so that while other setter is ignored, annotations does not mean that everything should be ignored (in absence of other info, Jackson tries to minimize need for use of annotations; specifically so that both getter and setter do NOT need either @JsonIgnore or @JsonProperty, one is enough).

You may also need to annotate matching property (since it has name that associates it with setter/getter); can mark it as ignored. Otherwise Jackson may claim it does not know what to do with it (since there is both inclusion and ignoral annotation).

Hope this helps,

-+ Tatu +-

ps. Mailing lists at Codehaus are deprecated, so I will cc the new Google group that is to replace it



On Thu, Jun 12, 2014 at 1:10 PM, Dan Kaplan <da...@mirthcorp.com> wrote:
I'm using Jersey 1.17 with `com.sun.jersey.api.json.POJOMappingFeature` set to true and I'm POSTing an object that looks like this:

    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlRootElement
    public class ByteArrayWithOverloadedSetterObject {
    
        private byte[] data;
    
        public void setData(byte[] data) {
            this.data = data;
        }
    
        @XmlTransient
        @JsonIgnore
        public void setData(String data) {
            this.data = data.getBytes();
        }
    
        public byte[] getData() {
            return data;
        }
    }

My Resource has this method:


    @POST
    @Path("postByteArrayWithOverloadedSetterObject")
    public byte[] sendByteArrayWithOverloadedSetterObject(ByteArrayWithOverloadedSetterObject byteArrayWithOverloadedSetterObject) {
        if (byteArrayWithOverloadedSetterObject.getData() == null) {
            log.warn("data was null!");
        }
        return byteArrayWithOverloadedSetterObject.getData();
    }

And my client side unit tests are written like this:


    @Test
    public void whenSendingStringDataThenGetDataBack() {
        ByteArrayWithOverloadedSetterObject byteArrayWithOverloadedSetterObject = new ByteArrayWithOverloadedSetterObject();
        byteArrayWithOverloadedSetterObject.setData("foobar");
        byte[] data = new AuthClient().postJaxbToUrl(RestApiUriBuilder.TEST_REST_PATH + "/postByteArrayWithOverloadedSetterObject", byteArrayWithOverloadedSetterObject, byte[].class);
        assertNotNull(data);
    }

    @Test
    public void whenSendingByteDataThenGetDataBack() {
        ByteArrayWithOverloadedSetterObject byteArrayWithOverloadedSetterObject = new ByteArrayWithOverloadedSetterObject();
        byteArrayWithOverloadedSetterObject.setData(new byte[]{0, 1, 1, 0});
        byte[] data = new AuthClient().postJaxbToUrl(RestApiUriBuilder.TEST_REST_PATH + "/postByteArrayWithOverloadedSetterObject", byteArrayWithOverloadedSetterObject, byte[].class);
        assertNotNull(data);
    }

Both of these tests are failing with a message that says "postByteArrayWithOverloadedSetterObject returned a response status of 204 No Content" and the server is logging "data was null!" for each.  Why isn't the byte array data being serialized over the wire?  I'm sending this as JSON.  

If I comment out the `@XmlTransient`/`@JsonIgnore` annotations, I get this error: "java.lang.RuntimeException: Conflicting setter definitions for property "data"".

In my real situation, I can't easily modify the API of `ByteArrayWithOverloadedSetterObject`.  But since this is just a test, I can see that things work correctly if I rename the second setter and the object becomes this:

    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlRootElement
    public class ByteArrayWithOverloadedSetterObject {
    
        private byte[] data;
    
        public void setData(byte[] data) {
            this.data = data;
        }
    
        @XmlTransient
        @JsonIgnore
        public void setData2(String data) {  //RENAME HERE.  Now tests pass.
            this.data = data.getBytes();
        }
    
        public byte[] getData() {
            return data;
        }
    }

Since I can't change the API of `ByteArrayWithOverloadedSetterObject`, is there any other work around to this?  

--
Thanks,
Dan

CONFIDENTIALITY NOTICE: The information contained in this electronic transmission may be confidential. If you are not an intended recipient, be aware that any disclosure, copying, distribution or use of the information contained in this transmission is prohibited and may be unlawful. If you have received this transmission in error, please notify us by email reply and then erase it from your computer system.

Dan Kaplan

unread,
Jun 12, 2014, 4:36:07 PM6/12/14
to jackso...@googlegroups.com, us...@jackson.codehaus.org
Oops, I just posted this here myself... I didn't realize your reply would make it show up.  Hopefully a mod ignores it. Let me just reply to you:

I took your advice and that fixed the issue.  My code looks like this now:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement
public class ByteArrayWithOverloadedSetterObject {

    private byte[] data;

    @JsonProperty
    public void setData(byte[] data) {
        this.data = data;
    }

    @XmlTransient
    @JsonIgnore
    public void setData(String data) {
        this.data = data.getBytes();
    }

    public byte[] getData() {
        return data;
    }
}


Thank you

Tatu Saloranta

unread,
Jun 12, 2014, 4:38:42 PM6/12/14
to us...@jackson.codehaus.org, jackso...@googlegroups.com
Glad it got resolved!

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages