只返回Spring Boot中API中模型的特定字段。

huangapple go评论137阅读模式
英文:

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&lt;MyModel&gt; myModelList = new ArrayList&lt;&gt;();
mongoUserCollection.find().into(myModelList);



 class MyModel{
    public int id; 
    public String name;
    public String lastname;
    public int age;
// getter &amp; 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() {
     // 返回结果的逻辑
   }
}

以上代码将只返回namelastname字段。有关更多详细信息,请参阅此链接

英文:

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(&quot;/get&quot;)
   @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

huangapple
  • 本文由 发表于 2020年9月21日 01:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定