英文:
Return only specific fields of model from api in spring boot
问题
以下是您要求的翻译部分:
我正在编写一个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方法
}
我将myModelList显示为响应。在响应中,它显示了所有字段。如何仅显示特定的字段,例如只显示id和age。仅从数据库中选择id和age仍会在响应中显示模型的所有字段(name和lastname将显示为null)。除了创建新的ModelView类或将JsonIgnore设置为此模型之外,是否还有其他方法呢?
英文:
I am writing a spring boot api which fetches some data from db, store in model object & returns it. But I want to return only few fields of the model as api response.
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
    }
I am showing myModelList as response. In response its showing all the fields. How to show only specific fields like id and age only. Selecting only id & age from db will still show all fields of model in response (name & lastname will be shown as null).
Is there any way apart from creating new ModelView class or setting JsonIgnore as this model?
答案1
得分: 8
如果您要返回的模型将针对单个API,请使用@JsonIgnore,下面的示例将忽略响应中的id字段:
class MyModel{
    @JsonIgnore
    public int id; 
    public String name;
    public String lastname;
    public int age;
}
但是如果同一模型将用于不同的API,且每个API具有不同类型的结果,那么我强烈推荐使用@JsonView来处理这些情况。一个简单的示例如下(将考虑您问题中的MyModel):
创建一个名为 Views.java 的类,其中包含一个空接口:
public class Views {
    public interface MyResponseViews {};
}
在模型中:
class MyModel{
  public int id; 
  @JsonView(Views.MyResponseViews.class)
  public String name;
  @JsonView(Views.MyResponseViews.class)
  public String lastname;
  public int age;
}
最后,您需要在发送此响应的控制器中添加以下内容(假设这是您的控制器):
MyModelController.java:
class MyModelController {
   // 自动注入 MyModelService ...
   @GetMapping("/get")
   @JsonView(Views.MyResponseViews.class)
   public ResponseEntity get() {
     // 返回结果的逻辑
   }
}
以上代码将只返回name和lastname字段。有关更多详细信息,请参阅此链接。
英文:
If the model which you are returning is going to be specific to single API go with @JsonIgnore, the below example will ignore the id in the response
class MyModel{
    @JsonIgnore
    public int id; 
    public String name;
    public String lastname;
    public int age;
}
But let say the same model is going to be used for different API and each API has different type of result then I would highly recommend @JsonView to handle those. A simple example will be below (will consider MyModel from your question)
Create a class Views.java with an empty interface
public class Views {
    public interface MyResponseViews {};
}
In Model
class MyModel{
  public int id; 
  @JsonView(Views.MyResponseViews.class)
  public String name;
  @JsonView(Views.MyResponseViews.class)
  public String lastname;
  public int age;
}
Last thing you have to add to the controller that send this response (Assuming your controller here)
MyModelController.java
class MyModelController {
   // Autowiring MyModelService ...
   
   @GetMapping("/get")
   @JsonView(Views.MyResponseViews.class)
   public ResponseEntity get() {
     // Your logic to return the result
   }
}
The above will return only name and lastname
Refer this for more detail this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论