如何使用jackson映射动态json属性名称以进行spring rest模板请求

Posted

技术标签:

【中文标题】如何使用jackson映射动态json属性名称以进行spring rest模板请求【英文标题】:How to map a dynamic json property name with jackson for spring rest template request 【发布时间】:2021-07-01 19:19:15 【问题描述】:

我正在尝试使用休息模板发送请求。通常它可以工作,但我需要一个动态杰克逊对象来处理这种类型的数据


  "id": 1,
  "name": "Name Of role",
  "permissions": 
    "global": [],
    "station": 
      "ID": [
        "String",
        "String",
        "String",
        "String"
      ]
    
  

在这种情况下,我正在尝试动态更改属性“ID”,但我遇到了任何杰克逊的运气不好。 通常我用jsonchema2pojo 创建我的对象。我的对象实际上有点大。我做错了什么,但我看不出这个问题在哪里。或者我在谷歌上搜索错误的上下文。

另外,这里是为我的请求对象生成的代码:


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))));




【问题讨论】:

你能显示代表上面 json 的 JAVA POJO 吗?这将很容易理解和修复 您好我配置了上面的json并添加了生成的pojo。 Station 类中的@Deadpool。 ID 属性应该是动态的,例如它可以是 1 2 3 或任何其他字符串 【参考方案1】:

根据我的经验,如果您要使用动态 JSON 对象,如 @Deadpool 所说,您可以使用 Map,但我的代码有点不同,因为我使用的是 而不是。这取决于您的需求。此外,由于您使用的是 Spring Framework,因此使用 Lombok 库会更干净。从您的代码中,您知道如何使用注释。所以也许你可以使用这个代码。我确实在生产环境中实现了这段代码,直到现在都没有问题。

@Getter
@Setter
public class JSONRequestBody 

    Map<String, Object> data;
    

【讨论】:

【参考方案2】:

如果您正在动态生成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); 

       

【讨论】:

以上是关于如何使用jackson映射动态json属性名称以进行spring rest模板请求的主要内容,如果未能解决你的问题,请参考以下文章

如何根据json中的属性编写jackson反序列化器

如何使用 Jackson 和 MongoDB 传递 JSON 消息中的属性?

使用“key”和“value”属性反序列化 JSON 映射不适用于 Jackson

使用“key”和“value”属性反序列化JSON映射不适用于Jackson

使用 Jackson 根据 API 版本指定不同的 JSON 属性名称

如何在JAVA中用JACKSON映射一个JSON中的数组,这个数组可以包含不同类型的对象作为项目?