泽西/jackson@JsonIgnore在二传手

我有一个带有以下注释的类:

class A { public Map<String,List> references; @JsonProperty public Map<String,List> getReferences() { ... } @JsonIgnore public void setReferences(Map<String,List>) { } ... } } 

我尝试的是忽略反序列化的json。 但它不起作用。 始终当JSON String到达时,Jackson lib会填充references属性。 如果我只使用@JsonIgnore注释,则getter不起作用。 这个问题有什么解决方案吗?

谢谢

我认为有两个关键部分可以让你根据需要拥有“只读集合”。 首先,除了忽略setter之外,还要确保你的字段也标有@JsonIgnore

 class A { @JsonIgnore public Map> references; @JsonProperty public Map> getReferences() { ... } @JsonIgnore public void setReferences(Map>) { ... } } 

其次,为了防止getter被用作setter,请禁用USE_GETTERS_AS_SETTERSfunction:

 ObjectMapper mapper = new ObjectMapper(); mapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS); 

你必须确保在字段级别和setter上都有@JsonIgnore注释,并使用@JsonProperty注释getter。

 public class Echo { @Null @JsonIgnore private String doNotDeserialise; private String echo; @JsonProperty public String getDoNotDeserialise() { return doNotDeserialise; } @JsonIgnore public void setDoNotDeserialise(String doNotDeserialise) { this.doNotDeserialise = doNotDeserialise; } public String getEcho() { return echo; } public void setEcho(String echo) { this.echo = echo; } } @Controller public class EchoController { @ResponseBody @RequestMapping(value = "/echo", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) public Echo echo(@RequestBody @Valid Echo echo) { if (StringUtils.isEmpty(echo.getDoNotDeserialise())) { echo.setDoNotDeserialise("Value is set by the server, not by the client!"); } return echo; } } 
  • 如果你提交一个设置为“doNotDeserialise”值的JSON请求,当JSON被反序列化为一个对象时,它将被设置为null(如果不是我在该字段上放置一个validation约束,那么它将会出错)
  • 如果您将“doNotDeserialise”值设置为服务器上的某个值,那么它将被正确序列化为JSON并推送到客户端

我在我的getter上使用了@JsonIgnore并且它无法工作,我无法配置映射器(我使用的是Jackson Jaxrs提供程序)。 这对我有用:

 @JsonIgnoreProperties(ignoreUnknown = true, value = { "actorsAsString", "writersAsString", "directorsAsString", "genresAsString" }) 

我只能想到一个非jackson解决方案,使用一个没有映射引用的基类,然后转换为实际的类:

 // expect a B on an incoming request class B { // ... } // after the data is read, cast to A which will have empty references class A extends B { public Map> references; } 

如果您不想要它们,为什么甚至发送参考文献?

或者是您手中的传入数据,您只是想避免映射exception,告诉您jackson无法找到为传入引用设置的属性? 为此,我们使用了一个基类,我们所有的Json模型类都inheritance了这个类:

 public abstract class JsonObject { @JsonAnySetter public void handleUnknown(String key, Object value) { // for us we log an error if we can't map but you can skip that Log log = LogFactory.getLog(String.class); log.error("Error mapping object of type: " + this.getClass().getName()); log.error("Could not map key: \"" + key + "\" and value: \"" + "\"" + value.toString() + "\""); } 

然后在POJO中添加@JsonIgnoreProperties以便传入的属性转发到handleUnknown()

 @JsonIgnoreProperties class A extends JsonObject { // no references if you don't need them } 

编辑

这个SO线程描述了如何使用Mixins。 这可能是解决方案,如果你想保持你的结构完全一样,但我还没有尝试过。

从Jackson 2.6开始,使用JsonProperty#access()注释定义了read-only write-only属性的新方法。 建议使用单独的JsonIgnoreJsonProperty注释。

 @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Map> references;