繁体   English   中英

在Spring Boot中如何为请求生成JSON值?

[英]How are JSON values generated for requests in Spring boot?

在Entity类中,如果我有一个以get <something>()开头的函数,该函数返回一个常数,即使没有声明此类成员变量,它也包含在JSON响应中。 Static函数不会出现在生成的json中。

我不知道要搜索什么。

我想知道为什么会这样,其潜在机制是什么。

@JsonInclude(JsonInclude.Include.NON_NULL)
@Entity
public class AssignedBook {

  @Id
  @GeneratedValue( strategy = GenerationType.AUTO )
  private Integer id;
  @OneToOne
  private Book book;
  @OneToOne
  private Users user;

  public static Integer getSomeId() {
    return 8;
  }
  public Integer someAwesomeId() {
    return 8;
  }
}

我的repository代码是

@Query(value="select new AssignedBook(id, book) from AssignedBook ab where ab.user.id=:userId")
  public List<AssignedBook> findByIdAndBookId(Integer userId);

对于id=15和一些book值,这将返回json

[
    {
        "id": 15,
        "book": {
            "id": 6,
            "name": "Rage of Angels",
            "author": "Sidney Sheldon"
        },
        "someAwesomeId": 8
    }
]

感谢x80486的方向。

Spring boot使用Jackson ,该Jackson使用反射来匹配getXXX并映射它。

更好的解释可以在这里找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM