Jackson是按照标准的setter和getter来反序列化和序列化对象的,如果字段没有getter方法,不会返回该字段;如果没有定义字段但是定义了get开头的方法,也会根据驼峰命名返回get后面的字段(注:如果get后全是大写,会全转成小写返回)。
package com.example.demo.pojo;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 2538024552952056059L;
private Integer age;
private Long amount;
public Long getABC(){
return 10L;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.demo.controller;
import com.example.demo.pojo.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author xuzhipeng
* @date 2021/12/25
*/
@RestController
public class HelloController {
@GetMapping("/query")
public List<Student> query(){
List<Student> list = new ArrayList<>();
list.add(new Student());
return list;
}
}
image.png
网友评论