简体   繁体   中英

How can I customize json output from spring rest controller

I have a POJO class patient with the below properties:

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;

....

Now from my rest controller I need to send only name, phoneNo, email and address of patient in the json. I expect the json output as

{
   "check":"Success",
   "details":{
      "name":"Test User",
      "phoneNo":"9876544321",
      "email":"test@gmail.com",
      "address":"Address"
   }
}

Here check Success/Failure is just added as a flag only.

Just create another object and use it as the response of your restful controller;

public class PatientResponse implements Serializable {

    private static final long serialVersionUID = 2L;

    private Check check;
    private Detail details;

    // getter, setter, etc

    public static class Detail {

        private String name;
        private String phoneNo;
        private String email;
        private String address;

        // getters, setters, etc
    }

    public enum Check {
        Success, Failure
    }
}

& in controller

@RestController
public class PatientController {

    @GetMapping(...)
    public PatientResponse get(...) {
        Patient patient = ... // get patient somehow
        return mapPatientToResponse(patient);  // map Patient to PatientResponse here
    }
}

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

For your request

PatientDTO.java

public class PatientDTO {
    private Check check;

    @JsonIgnoreProperties(value = {"id", "dob", "username", "password"})
    private Object details;

    /* Getter & Setter */

    public enum Check {
        SUCCESS("Success"),
        FAILURE("Failure");

        private String name;

        Check(String name) {
            this.name = name;
        }

        @JsonValue
        public String getName() {
            return name;
        }
    }
}

Controller demo:

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/patient")
    public PatientDTO getPatient() {
        PatientDTO patientDTO = new PatientDTO();
        patientDTO.setCheck(PatientDTO.Check.SUCCESS);
        patientDTO.setDetails(new Patient());
        return patientDTO;
    }
}

A Better way

use http status


@JsonInclude(JsonInclude.Include.NON_NULL)
public class Patient {
    private long id;
    private String name;
    private Date dob;
    private String phoneNo;
    private String email;
    private Address address;
    private String username;
    private String password;

    /* Getter & Setter */
}

Controller demo:

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("patient")
    // or @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Patient> patient() {
        Patient patient = new Patient();
        patient.setId(123);
        patient.setName("123");
        patient.setEmail("demo@demo.com");
        patient.setPassword(null); // set to null to ignore password
        return ResponseEntity.ok(patient);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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