spring mongodb 仅发送非空字段
spring mongodb send only the not null fields
我有一篇文章文档class喜欢。
package com.document.feed.model;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.lang.NonNullFields;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
class Source {
private String id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Article {
@Id
private String id;
private Source source;
private String author;
private String title;
private String description;
private String url;
private String urlToImage;
private String publishedAt;
private String content;
private String country;
private String category;
// Vector of Tf-Idf weights.
private List<Double> v;
// Extra fields computed during ordering.
private double dot;
// public Article(String id, String author, Double dot) {
// this.id = id;
// this.author = author;
// this.dot = dot;
// }
}
我正在使用聚合管道 select 仅 author
和 dot
文档作为:
{
author: 1,
dot: 1
}
聚合是这样完成的:
Aggregation aggregation = newAggregation(Article.class,
aggregate("$project",
projection),
sort(Sort.Direction.DESC, "dot")
);
return mongoTemplate.aggregate(aggregation, "article", Article.class);
但我收到的 API 响应为:
{
"id": "5e137c67771a9880d1639b5d",
"source": null,
"author": "Asian News International",
"title": null,
"description": null,
"url": null,
"urlToImage": null,
"publishedAt": null,
"content": null,
"country": null,
"category": null,
"v": null,
"dot": 3.2454110250954025
},
我只想将非空字段作为输出。我可以通过只为必填字段定义一个新的 POJO class 来做到这一点,但是有没有办法在不定义新的 classes 的情况下做到这一点(如果投影是的参数,那将是一场噩梦仅 API)?
添加 jackson 注释 @JsonInclude 以删除空字段。
@JsonInclude(Include.NON_NULL)
我有一篇文章文档class喜欢。
package com.document.feed.model;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.lang.NonNullFields;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
class Source {
private String id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Article {
@Id
private String id;
private Source source;
private String author;
private String title;
private String description;
private String url;
private String urlToImage;
private String publishedAt;
private String content;
private String country;
private String category;
// Vector of Tf-Idf weights.
private List<Double> v;
// Extra fields computed during ordering.
private double dot;
// public Article(String id, String author, Double dot) {
// this.id = id;
// this.author = author;
// this.dot = dot;
// }
}
我正在使用聚合管道 select 仅 author
和 dot
文档作为:
{
author: 1,
dot: 1
}
聚合是这样完成的:
Aggregation aggregation = newAggregation(Article.class,
aggregate("$project",
projection),
sort(Sort.Direction.DESC, "dot")
);
return mongoTemplate.aggregate(aggregation, "article", Article.class);
但我收到的 API 响应为:
{
"id": "5e137c67771a9880d1639b5d",
"source": null,
"author": "Asian News International",
"title": null,
"description": null,
"url": null,
"urlToImage": null,
"publishedAt": null,
"content": null,
"country": null,
"category": null,
"v": null,
"dot": 3.2454110250954025
},
我只想将非空字段作为输出。我可以通过只为必填字段定义一个新的 POJO class 来做到这一点,但是有没有办法在不定义新的 classes 的情况下做到这一点(如果投影是的参数,那将是一场噩梦仅 API)?
添加 jackson 注释 @JsonInclude 以删除空字段。
@JsonInclude(Include.NON_NULL)