java - Java 对象的动态 Json 文件
问题描述
我有一个 java 类模型,它具有 getter/setter 方法以及枚举类型。我想公开 JSON 文件以及所有字段,默认情况下一个字段有值,其他字段为空。
如何使用 java 模型类创建动态文件。
package com.manifest;
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.JsonCreator;
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 com.fasterxml.jackson.annotation.JsonValue;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"@id",
"@type",
"dct:identifier"
})
@Generated("jsonschema2pojo")
public class DctMediaType {
/**
*
* (Required)
*
*/
@JsonProperty("@id")
private String id;
/**
*
* (Required)
*
*/
@JsonProperty("@type")
private DctMediaType.Type type;
@JsonProperty("dct:identifier")
private String dctIdentifier;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* (Required)
*
*/
@JsonProperty("@id")
public String getId() {
return id;
}
/**
*
* (Required)
*
*/
@JsonProperty("@id")
public void setId(String id) {
this.id = id;
}
public DctMediaType withId(String id) {
this.id = id;
return this;
}
/**
*
* (Required)
*
*/
@JsonProperty("@type")
public DctMediaType.Type getType() {
return type;
}
/**
*
* (Required)
*
*/
@JsonProperty("@type")
public void setType(DctMediaType.Type type) {
this.type = type;
}
public DctMediaType withType(DctMediaType.Type type) {
this.type = type;
return this;
}
@JsonProperty("dct:identifier")
public String getDctIdentifier() {
return dctIdentifier;
}
@JsonProperty("dct:identifier")
public void setDctIdentifier(String dctIdentifier) {
this.dctIdentifier = dctIdentifier;
}
public DctMediaType withDctIdentifier(String dctIdentifier) {
this.dctIdentifier = dctIdentifier;
return this;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public DctMediaType withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(DctMediaType.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
sb.append("id");
sb.append('=');
sb.append(((this.id == null)?"<null>":this.id));
sb.append(',');
sb.append("type");
sb.append('=');
sb.append(((this.type == null)?"<null>":this.type));
sb.append(',');
sb.append("dctIdentifier");
sb.append('=');
sb.append(((this.dctIdentifier == null)?"<null>":this.dctIdentifier));
sb.append(',');
sb.append("additionalProperties");
sb.append('=');
sb.append(((this.additionalProperties == null)?"<null>":this.additionalProperties));
sb.append(',');
if (sb.charAt((sb.length()- 1)) == ',') {
sb.setCharAt((sb.length()- 1), ']');
} else {
sb.append(']');
}
return sb.toString();
}
@Override
public int hashCode() {
int result = 1;
result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode()));
result = ((result* 31)+((this.dctIdentifier == null)? 0 :this.dctIdentifier.hashCode()));
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
result = ((result* 31)+((this.type == null)? 0 :this.type.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof DctMediaType) == false) {
return false;
}
DctMediaType rhs = ((DctMediaType) other);
return (((((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id)))&&((this.dctIdentifier == rhs.dctIdentifier)||((this.dctIdentifier!= null)&&this.dctIdentifier.equals(rhs.dctIdentifier))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.type == rhs.type)||((this.type!= null)&&this.type.equals(rhs.type))));
}
@Generated("jsonschema2pojo")
public enum Type {
DCT_MEDIA_TYPE("dct:MediaType");
private final String value;
private final static Map<String, DctMediaType.Type> CONSTANTS = new HashMap<String, DctMediaType.Type>();
static {
for (DctMediaType.Type c: values()) {
CONSTANTS.put(c.value, c);
}
}
Type(String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
@JsonValue
public String value() {
return this.value;
}
@JsonCreator
public static DctMediaType.Type fromValue(String value) {
DctMediaType.Type constant = CONSTANTS.get(value);
if (constant == null) {
throw new IllegalArgumentException(value);
} else {
return constant;
}
}
}
}
需要我的 JSON 输出文件,如下所示:
{
"@id":null,
"@type":"dct:MediaType"
"dct:identifier":null
}
我尝试了以下方式,但我得到了空的 json 对象 {}。
ObjectMapper dynamicMapper = new ObjectMapper();
dynamicMapper.enable(SerializationFeature.INDENT_OUTPUT);
dynamicMapper.setSerializationInclusion(Include.ALWAYS);
String jsonValue = dynamicMapper.writeValueAsString(new DctMediaType());
请提出一些创建动态 json 文件创建的指导。
解决方案
问题是你们都有@JsonInclude(JsonInclude.Include.NON_NULL)
注释告诉序列化程序跳过任何 NULL 值并从 JSON 输出中删除属性,并且您永远不会将字段初始化为 bean 中的任何非 null 值。
您正在传递new DctMediaType()
到映射器,但从不设置任何字段。
听起来如果您想要JSON 中的空值,那么您应该将注释更改为@JsonInclude(JsonInclude.Include.ALWAYS)
推荐阅读
- c - 如何将 PCHAR* 转换为 TCHAR*?
- node.js - 从我的 AWS Educate 账户获取 AWS accesskeyid 错误
- c++ - 订单统计树和一维点问题的运算时间复杂度
- python - 尝试让 pandas 读取我的 json 文件时出错
- python - 如何切片 OrderedDict?
- javascript - 我无法使用 cmd "npx create-react-app" 创建反应应用程序
- java - 缺少零参数构造函数
- docker - 为 Traefik 可以找到的 docker 容器运行 nomad 作业
- python - 在同一个函数中调用函数时,无法从函数返回变量
- java - Minecraft 1.16.4 Spigot 新的下界合金物品问题