如何为 spring rest 模板请求映射动态 json 属性 名称与 jackson
How to map a dynamic json property name with jackson for spring rest template request
我正在尝试使用其余模板发送请求。通常它可以工作,但我需要一个动态的杰克逊对象来处理这种类型的数据
{
"id": 1,
"name": "Name Of role",
"permissions": {
"global": [],
"station": {
"ID": [
"String",
"String",
"String",
"String"
]
}
}
}
在这种情况下,我试图动态更改 属性“ID”,但我对 jackson 的 anygetter 运气不佳。
通常我用 jsonchema2pojo 创建我的对象。我的物品实际上有点大。我做错了什么,但我看不出这个问题出在哪里。或者我在 google.
上搜索错误的上下文
此外,这里是为我的请求对象生成的代码:
package com.example;
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;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"name",
"permissions"
})
@Generated("jsonschema2pojo")
public class CreateRoleRequestDto {
@JsonProperty("id")
private Integer id;
@JsonProperty("name")
private String name;
@JsonProperty("permissions")
private Permissions permissions;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("permissions")
public Permissions getPermissions() {
return permissions;
}
@JsonProperty("permissions")
public void setPermissions(Permissions permissions) {
this.permissions = permissions;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(CreateRoleRequestDto.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("name");
sb.append('=');
sb.append(((this.name == null)?"<null>":this.name));
sb.append(',');
sb.append("permissions");
sb.append('=');
sb.append(((this.permissions == null)?"<null>":this.permissions));
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.name == null)? 0 :this.name.hashCode()));
result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode()));
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
result = ((result* 31)+((this.permissions == null)? 0 :this.permissions.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof CreateRoleRequestDto) == false) {
return false;
}
CreateRoleRequestDto rhs = ((CreateRoleRequestDto) other);
return (((((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name)))&&((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.permissions == rhs.permissions)||((this.permissions!= null)&&this.permissions.equals(rhs.permissions))));
}
}
package com.example;
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;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"global",
"station"
})
@Generated("jsonschema2pojo")
public class Permissions {
@JsonProperty("global")
private List<Object> global = null;
@JsonProperty("station")
private Station station;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("global")
public List<Object> getGlobal() {
return global;
}
@JsonProperty("global")
public void setGlobal(List<Object> global) {
this.global = global;
}
@JsonProperty("station")
public Station getStation() {
return station;
}
@JsonProperty("station")
public void setStation(Station station) {
this.station = station;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Permissions.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
sb.append("global");
sb.append('=');
sb.append(((this.global == null)?"<null>":this.global));
sb.append(',');
sb.append("station");
sb.append('=');
sb.append(((this.station == null)?"<null>":this.station));
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.station == null)? 0 :this.station.hashCode()));
result = ((result* 31)+((this.global == null)? 0 :this.global.hashCode()));
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Permissions) == false) {
return false;
}
Permissions rhs = ((Permissions) other);
return ((((this.station == rhs.station)||((this.station!= null)&&this.station.equals(rhs.station)))&&((this.global == rhs.global)||((this.global!= null)&&this.global.equals(rhs.global))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))));
}
}
package com.example;
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;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"ID"
})
@Generated("jsonschema2pojo")
public class Station {
@JsonProperty("ID")
private List<String> id = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("ID")
public List<String> getId() {
return id;
}
@JsonProperty("ID")
public void setId(List<String> id) {
this.id = id;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Station.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("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.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Station) == false) {
return false;
}
Station rhs = ((Station) other);
return (((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id)))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))));
}
}
如果您要动态生成 id
,我建议您使用 Map
public class RequestData {
private Map<String, String> slug;
public Map<String, List<String>> getSlug(){
return properties;
}
public void add(String property, String List<String> value){
properties.put(property, value);
}
}
根据我的经验,如果你要玩动态 JSON 对象,就像@Deadpool 说的,你可以使用 Map
,但我的代码有点不同,因为我使用 而不是 。这取决于您的需要。此外,由于您使用的是 Spring 框架,如果您使用的是 Lombok 库,它会更干净。从您的代码中,您知道如何使用注释。所以也许你可以使用这段代码。我确实在生产环境中实现了这段代码,到目前为止没有问题。
@Getter
@Setter
public class JSONRequestBody {
Map<String, Object> data;
}
我正在尝试使用其余模板发送请求。通常它可以工作,但我需要一个动态的杰克逊对象来处理这种类型的数据
{
"id": 1,
"name": "Name Of role",
"permissions": {
"global": [],
"station": {
"ID": [
"String",
"String",
"String",
"String"
]
}
}
}
在这种情况下,我试图动态更改 属性“ID”,但我对 jackson 的 anygetter 运气不佳。 通常我用 jsonchema2pojo 创建我的对象。我的物品实际上有点大。我做错了什么,但我看不出这个问题出在哪里。或者我在 google.
上搜索错误的上下文此外,这里是为我的请求对象生成的代码:
package com.example;
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;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"name",
"permissions"
})
@Generated("jsonschema2pojo")
public class CreateRoleRequestDto {
@JsonProperty("id")
private Integer id;
@JsonProperty("name")
private String name;
@JsonProperty("permissions")
private Permissions permissions;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("permissions")
public Permissions getPermissions() {
return permissions;
}
@JsonProperty("permissions")
public void setPermissions(Permissions permissions) {
this.permissions = permissions;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(CreateRoleRequestDto.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("name");
sb.append('=');
sb.append(((this.name == null)?"<null>":this.name));
sb.append(',');
sb.append("permissions");
sb.append('=');
sb.append(((this.permissions == null)?"<null>":this.permissions));
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.name == null)? 0 :this.name.hashCode()));
result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode()));
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
result = ((result* 31)+((this.permissions == null)? 0 :this.permissions.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof CreateRoleRequestDto) == false) {
return false;
}
CreateRoleRequestDto rhs = ((CreateRoleRequestDto) other);
return (((((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name)))&&((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.permissions == rhs.permissions)||((this.permissions!= null)&&this.permissions.equals(rhs.permissions))));
}
}
package com.example;
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;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"global",
"station"
})
@Generated("jsonschema2pojo")
public class Permissions {
@JsonProperty("global")
private List<Object> global = null;
@JsonProperty("station")
private Station station;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("global")
public List<Object> getGlobal() {
return global;
}
@JsonProperty("global")
public void setGlobal(List<Object> global) {
this.global = global;
}
@JsonProperty("station")
public Station getStation() {
return station;
}
@JsonProperty("station")
public void setStation(Station station) {
this.station = station;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Permissions.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
sb.append("global");
sb.append('=');
sb.append(((this.global == null)?"<null>":this.global));
sb.append(',');
sb.append("station");
sb.append('=');
sb.append(((this.station == null)?"<null>":this.station));
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.station == null)? 0 :this.station.hashCode()));
result = ((result* 31)+((this.global == null)? 0 :this.global.hashCode()));
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Permissions) == false) {
return false;
}
Permissions rhs = ((Permissions) other);
return ((((this.station == rhs.station)||((this.station!= null)&&this.station.equals(rhs.station)))&&((this.global == rhs.global)||((this.global!= null)&&this.global.equals(rhs.global))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))));
}
}
package com.example;
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;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"ID"
})
@Generated("jsonschema2pojo")
public class Station {
@JsonProperty("ID")
private List<String> id = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("ID")
public List<String> getId() {
return id;
}
@JsonProperty("ID")
public void setId(List<String> id) {
this.id = id;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Station.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("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.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Station) == false) {
return false;
}
Station rhs = ((Station) other);
return (((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id)))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))));
}
}
如果您要动态生成 id
,我建议您使用 Map
public class RequestData {
private Map<String, String> slug;
public Map<String, List<String>> getSlug(){
return properties;
}
public void add(String property, String List<String> value){
properties.put(property, value);
}
}
根据我的经验,如果你要玩动态 JSON 对象,就像@Deadpool 说的,你可以使用 Map
,但我的代码有点不同,因为我使用
@Getter
@Setter
public class JSONRequestBody {
Map<String, Object> data;
}