Home > Blockchain >  How to create a pojo file for json response in java with jackson
How to create a pojo file for json response in java with jackson

Time:08-09

Here is my json response from an API.

{
  "job_1": {
  "metaData": {},
  "extractedData": {
      "score_a": [1,2,3,4],
      "score_b": [40,40,72,136],
      "score_c": [3,3.5,3,2,3,9],
      "score_d": [1659752409254420,1659752416354536,1659752424695028,1659752431823242 ]
  }
  }
}

In response job_1 can be vary. maybe its job_3 or job_4.

How to create a pojo file for this response?

Thanks in advance.

CodePudding user response:

I modified the request so that each job is unlabeled in request body to be considered as an object in the list

The request is like this :

{
"jobs":[
 {
    "metaData": {},
    "extractedData": {
        "score_a": [
            1,
            2,
            3,
            4
        ],
        "score_b": [
            40,
            40,
            72,
            136
        ],
        "score_c": [
            3,
            3.5,
            3,
            2,
            3,
            9
        ],
        "score_d": [
            1659752409254420,
            1659752416354536,
            1659752424695028,
            1659752431823242
        ]
    }
},
     {
    "metaData": {},
    "extractedData": {
        "score_a": [
            1,
            2,
            3,
            4
        ],
        "score_b": [
            40,
            40,
            72,
            136
        ],
        "score_c": [
            3,
            3.5,
            3,
            2,
            3,
            9
        ],
        "score_d": [
            1659752409254420,
            1659752416354536,
            1659752424695028,
            1659752431823242
        ]
    }
}
]
}

With the addition annotation @JsonSerialize to metadata to allow display of an object that does not contain elements

public class response {
  List<job> jobs;

  public static class job {

    MetaData metaData;
    ExtractedData extractedData;

  @JsonSerialize
  public static class MetaData {

    //insert code

  }

  public static class ExtractedData {

     List<Integer> score_a;
     List<Integer> score_b;
     List<Integer> score_c;
     List<Long> score_d;

  }
 }
}

I tried it like this

@Controller
public class test {

  @RequestMapping(value = "/test", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<response> testPost(@RequestBody response res) {

    return ResponseEntity.ok(res);

  }

}

CodePudding user response:

Use HashMap to handle dynamic keys job_1 can vary. maybe it's job_3 or job_4.

public class response {
  HashMap<String, job> jobs;

  public static class job {

    MetaData metaData;
    ExtractedData extractedData;

  @JsonSerialize
  public static class MetaData {

    //insert code

  }

  public static class ExtractedData {

     List<Integer> score_a;
     List<Integer> score_b;
     List<Integer> score_c;
     List<Long> score_d;

  }
 }
}
  • Related