自定义@JsonProperty注释

时间:2014-09-19 09:18:40

标签: java json jackson jsonschema jsonschema2pojo

考虑json架构:

{
    "type":"object",
    "$schema":"http://json-schema.org/draft-04/schema",
    "required":false,
    "properties":{
        "inventory":{
            "type":"object",
            "required":false,
            "properties":{
                "count":{
                    "type":"number",
                    "required":false
                }
            }
        }
    }
}

使用jsonschema2pojo时,会生成相应的java对象。从中提取的是以下计数声明(下面的完整课程):

@JsonProperty("count")
private Double count;

package com.test.json;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "count"
})
public class Inventory{

    /**
     * 
     */
    @JsonProperty("count")
    private Double count;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * 
     * @return
     *     The count
     */
    @JsonProperty("count")
    public Double getCount() {
        return count;
    }

    /**
     * 
     * @param count
     *     The count
     */
    @JsonProperty("count")
    public void setCount(Double count) {
        this.arpu = count;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object other) {
        return EqualsBuilder.reflectionEquals(this, other);
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

如果JSON接口发生更改,count变为countOfItems,则JSON属性注释 AND java变量名称更改,这会影响getter和setter:

@JsonProperty("countOfItems")
private Double countOfItems;

@JsonProperty("countOfItems")
public Double getCountOfItems()

@JsonProperty("countOfItems")
public void setCountOfItems(Double countOfItems)

我希望能够自定义生成的类,以便实现java代码不必更改,即:

@JsonProperty("countOfItems")
private Double count;

@JsonProperty("countOfItems")
public Double getCount()

@JsonProperty("countOfItems")
public void setCount(Double count)

使用JSON架构和jsonschema2pojo是否支持这种功能?我在json schemajsonscema2pojo文档中找不到有关如何执行此操作的任何信息。

显然,这可以通过手工制作java对象来实现,但我宁愿继续使用生成的类。

0 个答案:

没有答案