我使用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;
}
}
我的资源有这种方法:
@POST
@Path("postByteArrayWithOverloadedSetterObject")
public byte[] sendByteArrayWithOverloadedSetterObject(ByteArrayWithOverloadedSetterObject byteArrayWithOverloadedSetterObject) {
if (byteArrayWithOverloadedSetterObject.getData() == null) {
log.warn("data was null!");
}
return byteArrayWithOverloadedSetterObject.getData();
}
我的客户端单元测试是这样编写的:
@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);
}
这两个测试都失败了,并且消息显示" postByteArrayWithOverloadedSetterObject返回的响应状态为204 No Content"并且服务器正在记录"数据为空!"为每个人。为什么不通过线路串行化字节数组数据?我将此作为JSON发送。
如果我注释掉@XmlTransient
/ @JsonIgnore
注释,我会收到此错误:" java.lang.RuntimeException:属性"数据"&的冲突setter定义#34;
在我的实际情况中,我无法轻易修改ByteArrayWithOverloadedSetterObject
的API。但是因为这只是一个测试,如果我重命名第二个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;
}
}
由于我无法更改ByteArrayWithOverloadedSetterObject
的API,是否还有其他解决方法?
答案 0 :(得分:5)
这是解决方案:
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;
}
}
现在我的测试通过了。