简体   繁体   English

我在一个物体上有一个重载的二传手,我怎么能告诉杰克逊忽略其中一个呢?

[英]I have an overloaded setter on an object, how can I tell Jackson to ignore one of them?

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: 我正在使用Jersey 1.17, com.sun.jersey.api.json.POJOMappingFeature设置为true,我正在发布一个如下所示的对象:

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!" 这两个测试都失败了,消息显示“postByteArrayWithOverloadedSetterObject返回的响应状态为204 No Content”,服务器正在记录“data was null!” for each. 为每个人。 Why isn't the byte array data being serialized over the wire? 为什么不通过线路对字节数组数据进行序列化? I'm sending this as JSON. 我发送这个作为JSON。

If I comment out the @XmlTransient / @JsonIgnore annotations, I get this error: "java.lang.RuntimeException: Conflicting setter definitions for property "data"". 如果我注释掉@XmlTransient / @JsonIgnore注释,我会收到此错误:“java.lang.RuntimeException:属性”data“”的冲突setter定义。

In my real situation, I can't easily modify the API of ByteArrayWithOverloadedSetterObject . 在我的实际情况中,我无法轻易修改ByteArrayWithOverloadedSetterObject的API。 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: 但由于这只是一个测试,我可以看到,如果我重命名第二个setter并且对象变成了这个,那么事情就能正常工作:

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? 由于我无法更改ByteArrayWithOverloadedSetterObject的API,还有其他工作吗?

This is the solution: 这是解决方案:

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 //I ADDED THIS
    public void setData(byte[] data) {
        this.data = data;
    }

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

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

Now my tests pass. 现在我的测试通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉jackson忽略我无法控制源代码的属性? - How can I tell jackson to ignore a property for which I don't have control over the source code? 如何使用FasterXML Jackson忽略我没有的对象 - How to ignore object that I don't have with fasterxml jackson 如何在Jersey / Jackson中创建用于设置对象的自定义setter? - How can I create a custom setter for setting objects in Jersey/Jackson? 如何测试对象是否是重载版本? - How can I test if an object is an overloaded version or not? 如何告诉杰克逊在反序列化期间忽略空对象? - How to tell Jackson to ignore empty object during deserialization? 如何告诉 Jackson 在序列化过程中忽略带有 EMPTY 或 NULL 字段的 Object? - How to tell Jackson to ignore a Object with EMPTY or NULL fields during serialization? 谁能告诉我如何将 getter 和 setter 放在下面的代码中,以及 getter 和 setter 到底做了什么 - Can anyone tell how can i put getter and setter on the below code, and what exactly getter and setter do 如何告诉 Jackson 在反序列化期间跳过无效记录? - How can I tell Jackson to skip invalid records during deserialization? 杰克逊XML。 如何忽略XML属性? - Jackson-XML. How can I ignore an XML attribute? 我该如何告诉Jackson ObjectMapper使用实际属性,而不是用驼峰式包围它们? - How do I tell Jackson ObjectMapper to use the actual properties instead of camel casing them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM