gpt4 book ai didi

java - 如何将动态 json 属性名称与 jackson 映射以用于 Spring Rest 模板请求

转载 作者:行者123 更新时间:2023-12-04 14:07:52 24 4
gpt4 key购买 nike

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

{
"id": 1,
"name": "Name Of role",
"permissions": {
"global": [],
"station": {
"ID": [
"String",
"String",
"String",
"String"
]
}
}
}
在这种情况下,我正在尝试动态更改属性“ID”,但我对 jackson 的任何人都不走运。
通常我用 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))));
}

}

最佳答案

如果您有 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);

}
}

关于java - 如何将动态 json 属性名称与 jackson 映射以用于 Spring Rest 模板请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66961323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com