【发布时间】:2021-01-06 22:36:17
【问题描述】:
我正在编写一个 spring boot api,它从数据库中获取一些数据,存储在模型对象中并返回它。但我只想返回模型的几个字段作为 api 响应。
List<MyModel> myModelList = new ArrayList<>();
mongoUserCollection.find().into(myModelList);
class MyModel{
public int id;
public String name;
public String lastname;
public int age;
// getter & setter of all properties
}
我将 myModelList 显示为响应。作为回应,它显示了所有字段。如何仅显示特定字段,例如 id 和 age。从 db 中仅选择 id 和 age 仍将显示模型的所有字段作为响应(名称和姓氏将显示为空)。 除了创建新的 ModelView 类或将 JsonIgnore 设置为这个模型之外,还有什么办法吗?
【问题讨论】:
-
这question 有帮助吗?
@JsonIgnore
标签: java spring-boot spring-mvc