Home > Software design >  How to directly read this JsonArray which comes in a RequestBody
How to directly read this JsonArray which comes in a RequestBody

Time:06-04

I have the following JSON:

{"certificates":[
    {
      "fileType": "pdf",
      "binaryFile": "dasdasdasdas",
      "owner": {
        "namePerson": "Diego Pérez",
        "documentType": "TI",
        "documentNumber": "1234556650"
      }
    },
    {
      "fileType": "pdf",
      "binaryFile": "dasdasdasdas",
      "owner": {
        "namePerson": "Juan Pérez",
        "documentType": "PS",
        "documentNumber": "1024556650"
      }
    }
  ]}

It is a JsonArray which contains CertificateObject but I can not directly read it as it, I had some errors so I had to do the following (receive it as an String and do conversion):

@PostMapping("/certificates")
    public ResponseEntity<String> postCertificates(@RequestBody String certificates)
            throws JsonMappingException, JsonProcessingException {

        JsonObject convertedObject = new Gson().fromJson(certificates, JsonObject.class);
        log.info(convertedObject.get("certificates"));
        List<CertificateObject> defunctionCertificates = new ObjectMapper().readValue(
                convertedObject.get("certificates").toString(), new TypeReference<List<CertificateObject>>() {
                });
        return ResponseEntity.ok("ok");
    }

The problem is that I would like to be able to read it directly as an array (java List) like so:

@PostMapping("/certificates")
    public ResponseEntity<String> postCertificates(@RequestBody List<CertificateObject> certificates)
            throws JsonMappingException, JsonProcessingException {
        // no need to do any conversion to the certificates
        return ResponseEntity.ok("ok");
    }

Please let me know if you need more details (the CertificateObject class or something else) to help me with this, thank you!

CodePudding user response:

I think you can have a wrapper class which could get you list of certificates.

@Getter //Lombok annotation
@Setter
@Builder
public class CertificatesWrapper{
     private List<Certificate> certificates;
}

Add this to your endpoint request body.

@PostMapping("/certificates")
    public ResponseEntity<String> postCertificates(@RequestBody CertificateWrapper certificateWrapper)
            throws JsonMappingException, JsonProcessingException {
        // no need to do any conversion to the certificates
List<Certificate> certs =    certificateWrapper.getCertificates(); // gives you certs
        return ResponseEntity.ok("ok");
    }

I think the better way would be to send the request as a list of jsob objects rather than json obj containing a list of json objects. If so you wouldn't require this wrapper.

[
    {
      "fileType": "pdf",
      "binaryFile": "dasdasdasdas",
      "owner": {
        "namePerson": "Diego Pérez",
        "documentType": "TI",
        "documentNumber": "1234556650"
      }
    },
    {
      "fileType": "pdf",
      "binaryFile": "dasdasdasdas",
      "owner": {
        "namePerson": "Juan Pérez",
        "documentType": "PS",
        "documentNumber": "1024556650"
      }
    }
  ]

I think this helps !!!.

CodePudding user response:

You can convert your body request to a JsonNode object and then read the selected JsonNode certificates property into a List<CertificateObject> list calling the ObjectMapper#readerFor method:

@PostMapping("/certificates")
    public ResponseEntity<String> postCertificates(@RequestBody JsonNode root) throws IOException {
        ObjectReader reader = new ObjectMapper().readerFor(new TypeReference<List<CertificateObject>>() {
        });
        //reading the certificates property into a list
        List<CertificateObject> list = reader.readValue(root.at("/certificates")); 
        return ResponseEntity.ok("ok");
    }
  • Related