使用@JsonView批注可能对您有用
public class Views { public static class Public { } public static class base { } }public class Employee { @JsonProperty("emplyee_id") @JsonView({View.Public.class,View.base.class}) private Integer id; @JsonProperty("emplyee_first_name") @JsonView(View.Public.class) private String firstName; @JsonProperty("emplyee_last_name") @JsonView(View.Public.class) private String lastName; @JsonProperty("emplyee_address") private String address; @JsonProperty("emplyee_age") private Byte age; @JsonProperty("emplyee_level") @JsonView(View.base.class) private Byte level; //getters and setters }在您的json响应中添加@JsonView(Public / base.class),它将基于jsonview批注返回
//requestmapping@JsonView(View.Public.class) public ResponseEntity<Employee> getEmployeeWithPublicView(){ //do something}响应:
{ "employee_id":101, "employee_first_name":"Alex", "employee_last_name":"Light", "employee_age":null, "employee_address":null}第二个
//requestmapping@JsonView(View.base.class) public ResponseEntity<Employee> getEmployeeWithbaseView(){ //do something}响应
{ "employee_id":101, "employee_level":5}


