Rest API ResponseEntity 向响应添加额外的属性
Rest API ResponseEntity add extra attribute to response
我正在尝试找出 GET 方法的响应 API,它是 returning 名为“attributesMap”的额外属性
响应实体return代码为returnResponseEntity.ok(文档); <code></p>
<p>文件模型class</p>
<pre><code> private String id;
private String format;
private List<Attribute> attributes;
private String type;
private String accessright;
public Document() {
}
/**
* Copy constructor.
*/
public Document(Document source) {
this.id = source.id;
this.format = source.format;
this.type = source.type;
this.accessright = source.accessright;
this.attributes = source.attributes;
}
public static Document newDocument(String type, String id, String format, String accessright, List<Attribute> attributes) {
Document document = new Document();
document.type = type;
document.id = id;
document.format = format;
document.accessright = accessright;
document.attributes = attributes;
return document;
}
public static Document newCompleteDocument(String id, String type, String format, String accessright, List<Attribute> attributes) {
Document document = new Document();
document.id = id;
document.type = type;
document.format = format;
document.accessright = accessright;
document.attributes = attributes;
return document;
}
//
// Getters and setters ahead
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
public String getAccessright() {
return accessright;
}
public void setAccessright(String accessright) {
this.accessright = accessright;
}
public Map<String, List<String>> getAttributesMap() {
Map<String, List<String>> attributesMap = getAttributes().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValues));
return attributesMap;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Document{");
sb.append("id='").append(id).append('\'');
sb.append(", type='").append(type).append('\'');
sb.append(", format='").append(format).append('\'');
sb.append(", accessright='").append(accessright).append('\'');
sb.append(", attributes=[").append(attributes);
if (attributes != null) {
for (Attribute attribute:attributes) {
sb.append(attribute.getName()).append(":").append(attribute.getValues().toArray()).append(",");
}
}
sb.append("]}");
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Document document = (Document) o;
if (id != null ? !id.equals(document.id) : document.id != null) {
return false;
}
if (type != null ? !type.equals(document.type) : document.type != null) {
return false;
}
if (format != null ? !format.equals(document.format) : document.format != null) {
return false;
}
if (accessright != null ? !accessright.equals(document.accessright) : document.accessright != null) {
return false;
}
return attributes != null ? attributes.equals(document.attributes) : document.attributes == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (format != null ? format.hashCode() : 0);
result = 31 * result + (accessright != null ? accessright.hashCode() : 0);
result = 31 * result + (attributes != null ? attributes.hashCode() : 0);
return result;
}
}
但是 API 响应 JSON 作为一个额外的属性“attributesMap”。示例 JSON 如下:
{
"id": "xxx",
“格式”:“xx”,
“属性”: [
{
“名称”:“属性1”,
“价值观”:[
“val1”
]
}
],
“类型”:“测试类型”,
“访问权限”:“删除”,
“属性图”:{
“名称”:“属性1”,
“价值观”:[
“val1”
]
}
}
当我调试到 return 时,谁能帮我弄清楚如何检查这个属性的来源?
return ResponseEntity.ok(文档);
文档模型对象中没有 attributesMap。
您遇到的问题是 getter getAttributesMap
。 jackson 默认会序列化什么?
- 所有 public 个字段
- 所有 getter 方法
你能做什么?
- 重命名方法
- 用
@JsonIgnore
排除 getter
@JsonIgnore
public Map<String, List<String>> getAttributesMap() {
Map<String, List<String>> attributesMap = getAttributes().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValues));
return attributesMap;
}
您还可以提供具有以下设置的您自己的 ObjectMapper bean:
var mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
或
var mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
这只会序列化字段,而不是 getters。
我正在尝试找出 GET 方法的响应 API,它是 returning 名为“attributesMap”的额外属性
响应实体return代码为returnResponseEntity.ok(文档); <code></p>
<p>文件模型class</p>
<pre><code> private String id;
private String format;
private List<Attribute> attributes;
private String type;
private String accessright;
public Document() {
}
/**
* Copy constructor.
*/
public Document(Document source) {
this.id = source.id;
this.format = source.format;
this.type = source.type;
this.accessright = source.accessright;
this.attributes = source.attributes;
}
public static Document newDocument(String type, String id, String format, String accessright, List<Attribute> attributes) {
Document document = new Document();
document.type = type;
document.id = id;
document.format = format;
document.accessright = accessright;
document.attributes = attributes;
return document;
}
public static Document newCompleteDocument(String id, String type, String format, String accessright, List<Attribute> attributes) {
Document document = new Document();
document.id = id;
document.type = type;
document.format = format;
document.accessright = accessright;
document.attributes = attributes;
return document;
}
//
// Getters and setters ahead
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
public String getAccessright() {
return accessright;
}
public void setAccessright(String accessright) {
this.accessright = accessright;
}
public Map<String, List<String>> getAttributesMap() {
Map<String, List<String>> attributesMap = getAttributes().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValues));
return attributesMap;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Document{");
sb.append("id='").append(id).append('\'');
sb.append(", type='").append(type).append('\'');
sb.append(", format='").append(format).append('\'');
sb.append(", accessright='").append(accessright).append('\'');
sb.append(", attributes=[").append(attributes);
if (attributes != null) {
for (Attribute attribute:attributes) {
sb.append(attribute.getName()).append(":").append(attribute.getValues().toArray()).append(",");
}
}
sb.append("]}");
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Document document = (Document) o;
if (id != null ? !id.equals(document.id) : document.id != null) {
return false;
}
if (type != null ? !type.equals(document.type) : document.type != null) {
return false;
}
if (format != null ? !format.equals(document.format) : document.format != null) {
return false;
}
if (accessright != null ? !accessright.equals(document.accessright) : document.accessright != null) {
return false;
}
return attributes != null ? attributes.equals(document.attributes) : document.attributes == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (format != null ? format.hashCode() : 0);
result = 31 * result + (accessright != null ? accessright.hashCode() : 0);
result = 31 * result + (attributes != null ? attributes.hashCode() : 0);
return result;
}
}
但是 API 响应 JSON 作为一个额外的属性“attributesMap”。示例 JSON 如下:
{ "id": "xxx", “格式”:“xx”, “属性”: [ { “名称”:“属性1”, “价值观”:[ “val1” ] } ], “类型”:“测试类型”, “访问权限”:“删除”, “属性图”:{ “名称”:“属性1”, “价值观”:[ “val1” ] } }
当我调试到 return 时,谁能帮我弄清楚如何检查这个属性的来源? return ResponseEntity.ok(文档); 文档模型对象中没有 attributesMap。
您遇到的问题是 getter getAttributesMap
。 jackson 默认会序列化什么?
- 所有 public 个字段
- 所有 getter 方法
你能做什么?
- 重命名方法
- 用
@JsonIgnore
排除 getter
@JsonIgnore
public Map<String, List<String>> getAttributesMap() {
Map<String, List<String>> attributesMap = getAttributes().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValues));
return attributesMap;
}
您还可以提供具有以下设置的您自己的 ObjectMapper bean:
var mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
或
var mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
这只会序列化字段,而不是 getters。