从jsonschema2pojo生成的POJO生成不应存在的类

时间:2015-06-13 06:40:15

标签: java javascript json jsonschema2pojo

我在命令行中使用jsonschema2pojo为以下json创建POJO:

book_store.json

{
	"type": "object",
	"$schema": "http://json-schema.org/draft-04/schema",
	"description": "Books that I have",
	"properties": {
				"soft-copies": {
					"type": "array",
					"items": {
						"$ref": "./book_details.json"
					}
				},
				"hard-copies": {
					"type": "array",
					"items": {
						"$ref": "./book_details.json"
					}
				}
		}
}

book_details.json

{
	"type": "object",
	"$schema": "http://json-schema.org/draft-04/schema",
	"description": "attributes of a book",
	"properties": {
		"id": {
			"type": "string",
			"description": "ID of the parameteror category or channel or file"
		},

		"code": {
			"type": "string",
			"description": "key of the parameter or category or channel or file"
		},

		"description": {
			"type": "string",
			"description": "description of the parameter or category or channel or file"
		}
	}
}

现在生成的Java POJO看起来像这样:

BookStore.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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;


/**
 * Books that I have
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "soft-copies",
    "hard-copies"
})
public class BookStore {

    @JsonProperty("soft-copies")
    private List<SoftCopy> softCopies = new ArrayList<SoftCopy>();
    @JsonProperty("hard-copies")
    private List<SoftCopy> hardCopies = new ArrayList<SoftCopy>();
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * 
     * @return
     *     The softCopies
     */
    @JsonProperty("soft-copies")
    public List<SoftCopy> getSoftCopies() {
        return softCopies;
    }

    /**
     * 
     * @param softCopies
     *     The soft-copies
     */
    @JsonProperty("soft-copies")
    public void setSoftCopies(List<SoftCopy> softCopies) {
        this.softCopies = softCopies;
    }

    /**
     * 
     * @return
     *     The hardCopies
     */
    @JsonProperty("hard-copies")
    public List<SoftCopy> getHardCopies() {
        return hardCopies;
    }

    /**
     * 
     * @param hardCopies
     *     The hard-copies
     */
    @JsonProperty("hard-copies")
    public void setHardCopies(List<SoftCopy> hardCopies) {
        this.hardCopies = hardCopies;
    }

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

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

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

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(softCopies).append(hardCopies).append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof BookStore) == false) {
            return false;
        }
        BookStore rhs = ((BookStore) other);
        return new EqualsBuilder().append(softCopies, rhs.softCopies).append(hardCopies, rhs.hardCopies).append(additionalProperties, rhs.additionalProperties).isEquals();
    }

}

SoftCopy.java

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;


/**
 * attributes of a book
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "id",
    "code",
    "description"
})
public class SoftCopy {

    /**
     * ID of the parameteror category or channel or file
     * 
     */
    @JsonProperty("id")
    private String id;
    /**
     * key of the parameter or category or channel or file
     * 
     */
    @JsonProperty("code")
    private String code;
    /**
     * description of the parameter or category or channel or file
     * 
     */
    @JsonProperty("description")
    private String description;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * ID of the parameteror category or channel or file
     * 
     * @return
     *     The id
     */
    @JsonProperty("id")
    public String getId() {
        return id;
    }

    /**
     * ID of the parameteror category or channel or file
     * 
     * @param id
     *     The id
     */
    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }

    /**
     * key of the parameter or category or channel or file
     * 
     * @return
     *     The code
     */
    @JsonProperty("code")
    public String getCode() {
        return code;
    }

    /**
     * key of the parameter or category or channel or file
     * 
     * @param code
     *     The code
     */
    @JsonProperty("code")
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * description of the parameter or category or channel or file
     * 
     * @return
     *     The description
     */
    @JsonProperty("description")
    public String getDescription() {
        return description;
    }

    /**
     * description of the parameter or category or channel or file
     * 
     * @param description
     *     The description
     */
    @JsonProperty("description")
    public void setDescription(String description) {
        this.description = description;
    }

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

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

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

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).append(code).append(description).append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof SoftCopy) == false) {
            return false;
        }
        SoftCopy rhs = ((SoftCopy) other);
        return new EqualsBuilder().append(id, rhs.id).append(code, rhs.code).append(description, rhs.description).append(additionalProperties, rhs.additionalProperties).isEquals();
    }

}

虽然我甚至没想到SoftCopy.java对象...我希望有“BookStore.java”&amp; “BookDetails.java”。 SoftCopy和HardCopy应该是BookDetails.java类型的列表

1 个答案:

答案 0 :(得分:0)

使用扩展程序“javaType” - https://github.com/joelittlejohn/jsonschema2pojo/issues/164

解决了此问题