我正在尝试找出 GET 方法 API 的响应,它返回名为“attributesMap”的额外属性
响应实体返回码为 return ResponseEntity.ok(document);
document model class but the API reponse JSON as an extra attribute "attributesMap". Sample JSON as below:
文档模型对象中没有 attributesMap。
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;
}
}
{
"id": "xxx",
"format": "xx",
"attributes": [
{
"name": "attr1",
"values": [
"val1"
]
}
],
"type": "test type",
"accessright": "DELETE",
"attributesMap": {
"name": "attr1",
"values": [
"val1"
]
}
}
答案 0 :(得分:1)
你的问题是你有 getter getAttributesMap
。 jackson 默认会序列化什么?
你能做什么?
@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);
这只会序列化字段,而不是 getter。