如何自定义 spring rest 控制器的 json 输出

我有一个具有以下属性的 POJO 类患者:


public class Patient implements Serializable{


    private static final long serialVersionUID = 2L;


    private long id;

    private String name;

    private Date dob;

    private String phoneNo;

    private String email;

    private Address address;

    private String username;

    private String password;


....

现在,从我的休息控制器中,我只需要在 json 中发送患者的姓名、电话号码、电子邮件和地址。我希望 json 输出为


{

   "check":"Success",

   "details":{

      "name":"Test User",

      "phoneNo":"9876544321",

      "email":"test@gmail.com",

      "address":"Address"

   }

}

此处检查成功/失败仅作为标志添加。


www说
浏览 115回答 3
3回答

蝴蝶刀刀

Spring boot 使用 Jackson 进行 JSON 序列化和反序列化,尝试使用 @JSONIgnore (com.fasterxml.jackson.annotation.JsonIgnore)。

qq_笑_17

为您的要求PatientDTO.javapublic class PatientDTO {&nbsp; &nbsp; private Check check;&nbsp; &nbsp; @JsonIgnoreProperties(value = {"id", "dob", "username", "password"})&nbsp; &nbsp; private Object details;&nbsp; &nbsp; /* Getter & Setter */&nbsp; &nbsp; public enum Check {&nbsp; &nbsp; &nbsp; &nbsp; SUCCESS("Success"),&nbsp; &nbsp; &nbsp; &nbsp; FAILURE("Failure");&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; Check(String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @JsonValue&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}控制器演示:@RestController@RequestMapping("/test")public class TestController {&nbsp; &nbsp; @GetMapping("/patient")&nbsp; &nbsp; public PatientDTO getPatient() {&nbsp; &nbsp; &nbsp; &nbsp; PatientDTO patientDTO = new PatientDTO();&nbsp; &nbsp; &nbsp; &nbsp; patientDTO.setCheck(PatientDTO.Check.SUCCESS);&nbsp; &nbsp; &nbsp; &nbsp; patientDTO.setDetails(new Patient());&nbsp; &nbsp; &nbsp; &nbsp; return patientDTO;&nbsp; &nbsp; }}更好的方法使用 http 状态@JsonInclude(JsonInclude.Include.NON_NULL)public class Patient {&nbsp; &nbsp; private long id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private Date dob;&nbsp; &nbsp; private String phoneNo;&nbsp; &nbsp; private String email;&nbsp; &nbsp; private Address address;&nbsp; &nbsp; private String username;&nbsp; &nbsp; private String password;&nbsp; &nbsp; /* Getter & Setter */}控制器演示:@RestController@RequestMapping("/test")public class TestController {&nbsp; &nbsp; @GetMapping("patient")&nbsp; &nbsp; // or @ResponseStatus(HttpStatus.OK)&nbsp; &nbsp; public ResponseEntity<Patient> patient() {&nbsp; &nbsp; &nbsp; &nbsp; Patient patient = new Patient();&nbsp; &nbsp; &nbsp; &nbsp; patient.setId(123);&nbsp; &nbsp; &nbsp; &nbsp; patient.setName("123");&nbsp; &nbsp; &nbsp; &nbsp; patient.setEmail("demo@demo.com");&nbsp; &nbsp; &nbsp; &nbsp; patient.setPassword(null); // set to null to ignore password&nbsp; &nbsp; &nbsp; &nbsp; return ResponseEntity.ok(patient);&nbsp; &nbsp; }}

明月笑刀无情

只需创建另一个对象并将其用作您的 restful 控制器的响应;public class PatientResponse implements Serializable {&nbsp; &nbsp; private static final long serialVersionUID = 2L;&nbsp; &nbsp; private Check check;&nbsp; &nbsp; private Detail details;&nbsp; &nbsp; // getter, setter, etc&nbsp; &nbsp; public static class Detail {&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; private String phoneNo;&nbsp; &nbsp; &nbsp; &nbsp; private String email;&nbsp; &nbsp; &nbsp; &nbsp; private String address;&nbsp; &nbsp; &nbsp; &nbsp; // getters, setters, etc&nbsp; &nbsp; }&nbsp; &nbsp; public enum Check {&nbsp; &nbsp; &nbsp; &nbsp; Success, Failure&nbsp; &nbsp; }}& 在控制器中@RestControllerpublic class PatientController {&nbsp; &nbsp; @GetMapping(...)&nbsp; &nbsp; public PatientResponse get(...) {&nbsp; &nbsp; &nbsp; &nbsp; Patient patient = ... // get patient somehow&nbsp; &nbsp; &nbsp; &nbsp; return mapPatientToResponse(patient);&nbsp; // map Patient to PatientResponse here&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java