0

In the code mentioned below,

String additionalAttributes = new ObjectMapper().writeValueAsString(retrieveAttributes(requestApplication));

"additionalAttributes" is giving me first json response and sessionValidationResponse is giving me another json response, please note that both are having different return types(String and SessionValidationResponse ) how do I merge these responses to send a single json response

public SessionValidationResponse checkToken(String tokenId) throws RuntimeException {
    System.out.println(" \n \n \n Enter in checkToken method");
    System.out.println("Received Token ID: " + tokenId);


    if (null != tokenId && tokenId != "") {
        try {


            String filepath = "session_validation.json";
            String requestApplication = "myVf";
            String additionalAttributes = new ObjectMapper().writeValueAsString(retrieveAttributes(requestApplication));
            System.out.println("additionalAttributes: " +additionalAttributes);


            try {
                SessionValidationResponse sessionValidationResponse = jsonFileReader.read(filepath, new TypeReference<SessionValidationResponse>() {
                });
                sessionValidationResponse.setToken(createToken(sessionValidationResponse.getUserName(), new Date(), Calendar.getInstance().getTime()));
                return sessionValidationResponse;
            } catch (Exception e) {
                throw new RuntimeException();
            }


        } catch (SignatureException e) {
            throw new InvalidTokenException(new Exception(INVALID_TOKEN));
        } catch (ExpiredJwtException e) {
            throw new TokenExpirationException(new Exception(TOKEN_EXPIRED));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    } else {
        throw new TokenEmptyException(new Exception(TOKEN_NOT_FOUND));
    }
    return null;
}

Response of String type: {"status":"ACTIVE","msisdn":"45695","UT_MOBILE":"4258963144","gender":"MALE"}

Response of SessionValidationResponse type

{
  "valid": true,
  "uid": "demo",
  "userName": "ABC",
  "token": "randomToken",
  "msisdn": "9949999499"
}
Pratik Ambani
  • 2,523
  • 1
  • 18
  • 28
  • You mean [like this](http://stackoverflow.com/questions/9895041/merging-two-json-documents-using-jackson)? – dnault Dec 14 '16 at 20:47
  • @dnault Kinda, but example shows both objects having same return types(MyBean) and I am having two different types (String and SessionValidationResponse ). Thank you for the revert. :) – Pratik Ambani Dec 15 '16 at 18:35

1 Answers1

0

Found the solution. I casted both the values into a HashMap and merged them into a single (new)Map using putAll(map1, map2); using Map.

Pratik Ambani
  • 2,523
  • 1
  • 18
  • 28