0

Here I'm having a controller which is of postmapping type. I'm taking the request body as String which a nested JSON. When the controller is being called with the String as request body I want to map that String into a POJO. In that POJO I have fields which are to be mapped from that nested json and also a field which takes the actual String request body as is. Kindly help me how do I map a particular field from that nested json string to the POJO.

The request looks like -

{
    "Application": {
        "DCode": "unsecliverelease",
        "PType": "DA",
        "AId": "230391106",
        "ApNO": "NTFLbjOF9fXI15AF1YiC",
        "crd": {
            "cate": "lion",
            "ProductCode": "lion"
        },
        "ld": {
            "dm": {
                "sn": "3",
                "RandomNumbers": {
                    "RandomNumber01": "319",
                    "RandomNumber02": "731",
                    "RandomNumber03": "520",
                    "RandomNumber04": "102",
                    "RandomNumber05": "678"
                },
                "Request": {
                    "Name": "MSE",
                    "ACount": "1",
                    "BrandInd": "wert",
                    "CID": "123456789",
                    
                }
            }
    }

//controller

@PostMapping(
      value = "/decision",
      produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<crdResponse > getDecision(
      @RequestBody final @Valid String request) throws JsonProcessingException {
    crdResponse response =
        crdService.getDec(request);

    return ResponseEntity.ok().body(response);
  }

//POJO

public class CRequestModel {

  @Column(name = "rid")
  @Id
  private String crdRqsId;

  @Column(name = "scode")
  private String scode;

  @Column(name = "cid")
  private Integer cid;

  @Column(name = "RequestNumber")
  private Integer requestNumber;

  @Column(name = "RequestJson")
  private String requestJSON;

  @Column(name = "CreatedAt")
  private Timestamp createdAt;
}

I want to save the whole JSON string into requestJSON field and want to save CID value(from request JSON STRING) into cid field.

Kindly help me with the same. This input JSON String can change so the order in which the CID is there in current JSON String might vary.

2 Answers 2

1

Json you posted is not valid. I guess it should look like this :

{
  "Application": {
    "DCode": "unsecliverelease",
    "PType": "DA",
    "AId": "230391106",
    "ApNO": "NTFLbjOF9fXI15AF1YiC",
    "crd": {
      "cate": "lion",
      "ProductCode": "lion"
    },
    "ld": {
      "dm": {
        "sn": "3",
        "RandomNumbers": {
          "RandomNumber01": "319",
          "RandomNumber02": "731",
          "RandomNumber03": "520",
          "RandomNumber04": "102",
          "RandomNumber05": "678"
        },
        "Request": {
          "Name": "MSE",
          "ACount": "1",
          "BrandInd": "wert",
          "CID": "123456789"
        }
      }
    }
  }
}

What you can do is the following:

String json ="{\n" +
                     "  \"Application\": {\n" +
                     "    \"DCode\": \"unsecliverelease\",\n" +
                     "    \"PType\": \"DA\",\n" +
                     "    \"AId\": \"230391106\",\n" +
                     "    \"ApNO\": \"NTFLbjOF9fXI15AF1YiC\",\n" +
                     "    \"crd\": {\n" +
                     "      \"cate\": \"lion\",\n" +
                     "      \"ProductCode\": \"lion\"\n" +
                     "    },\n" +
                     "    \"ld\": {\n" +
                     "      \"dm\": {\n" +
                     "        \"sn\": \"3\",\n" +
                     "        \"RandomNumbers\": {\n" +
                     "          \"RandomNumber01\": \"319\",\n" +
                     "          \"RandomNumber02\": \"731\",\n" +
                     "          \"RandomNumber03\": \"520\",\n" +
                     "          \"RandomNumber04\": \"102\",\n" +
                     "          \"RandomNumber05\": \"678\"\n" +
                     "        },\n" +
                     "        \"Request\": {\n" +
                     "          \"Name\": \"MSE\",\n" +
                     "          \"ACount\": \"1\",\n" +
                     "          \"BrandInd\": \"wert\",\n" +
                     "          \"CID\": \"123456789\"\n" +
                     "        }\n" +
                     "      }\n" +
                     "    }\n" +
                     "  }\n" +
                     "}";
    
    

CRequestModel cRequestModel = new CRequestModel();
    cRequestModel.setRequestJSON(json);

    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree(json);
    JsonNode cidNode = actualObj.get("Application").get("ld").get("dm").get("Request").get("CID");
    int cid= mapper.treeToValue(cidNode,Integer.class);
    cRequestModel.setCid(cid);

NOTE: The assumption is that the json you get will always have structure above and propeties: ld, dm, Request,CID. (order doesn't matter)

Sign up to request clarification or add additional context in comments.

Comments

1

If you have JSON string that can change, I think you have to create a method that parses that JSON string into a jsonobject and then extract a value from a specified "key".

For example, if you have a JSON string so composed,

{
 "name":"Sam", 
 "surname":"Baudo"
}

String jsonStringInput = "input-json-value"; 
JSONObject json = new JSONObject(jsonStringInput);
String surname = json.getString("surname");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.