When I tried to send a GET request I am getting JSON response as below:
[{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"1001","customerInfoReferenceNumber":null,"externalSystemId":null,"id":"1001","lastUpdatedTime":null,"lastUpdatedUser":"User1001","leadContact":false,"loanInformation":null,"orphanFlag":"N","preferredCurrency":"USD","recoveryAccountingStatus":null,"relationshipType":"B","responsibleParty":true,"state":"PENDING"},{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"1002","customerInfoReferenceNumber":null,"externalSystemId":null,"id":"1002","lastUpdatedTime":null,"lastUpdatedUser":"User1002","leadContact":false,"loanInformation":null,"orphanFlag":"N","preferredCurrency":"USD","recoveryAccountingStatus":null,"relationshipType":"C","responsibleParty":true,"state":"PENDING"},{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"1003","customerInfoReferenceNumber":null,"externalSystemId":null,"id":"1003","lastUpdatedTime":null,"lastUpdatedUser":"User1003","leadContact":false,"loanInformation":null,"orphanFlag":"N","preferredCurrency":"USD","recoveryAccountingStatus":null,"relationshipType":"P","responsibleParty":true,"state":"PENDING"},{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"1004","customerInfoReferenceNumber":null,"externalSystemId":null,"id":"1004","lastUpdatedTime":null,"lastUpdatedUser":"User1004","leadContact":false,"loanInformation":null,"orphanFlag":"N","preferredCurrency":"USD","recoveryAccountingStatus":null,"relationshipType":"O","responsibleParty":true,"state":"PENDING"},{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"1033","customerInfoReferenceNumber":null,"externalSystemId":null,"id":"1000","lastUpdatedTime":null,"lastUpdatedUser":"User1000","leadContact":true,"loanInformation":null,"orphanFlag":"N","preferredCurrency":"USD","recoveryAccountingStatus":null,"relationshipType":"A","responsibleParty":true,"state":"PENDING"},{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"5604","customerInfoReferenceNumber":null,"externalSystemId":"FTES","id":"230633","lastUpdatedTime":null,"lastUpdatedUser":"manglayaan.usr","leadContact":false,"loanInformation":null,"orphanFlag":"N","preferredCurrency":null,"recoveryAccountingStatus":null,"relationshipType":"Q","responsibleParty":false,"state":"PENDING"},{"accountId":"1000","applicationNumber":null,"bankNumber":null,"contactDetails":null,"contactId":"23060","customerInfoReferenceNumber":null,"externalSystemId":null,"id":"SAjnW8lduCcvzlRVHEjB","lastUpdatedTime":null,"lastUpdatedUser":"SYSTEM","leadContact":false,"loanInformation":null,"orphanFlag":"N","preferredCurrency":null,"recoveryAccountingStatus":null,"relationshipType":"A","responsibleParty":true,"state":"PENDING"}]
My code is:
@Component
public class TokenizationFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
List<String> sensitiveFields = Arrays.asList("accountId", "govt_id");
HttpServletResponse httpResponse = (HttpServletResponse) response;
ContentCachingResponseWrapper readWrapper = new ContentCachingResponseWrapper(httpResponse);
HttpServletResponseWrapper writeWrapper = new HttpServletResponseWrapper(httpResponse);
chain.doFilter(request, readWrapper);
String content =
new String(readWrapper.getContentAsByteArray(), readWrapper.getCharacterEncoding());
System.out.println("Content: " content);
try {
JSONObject jsonObject = new JSONObject(content);
//JSONArray jsonObject = new JSONArray(content);
sensitiveFields.forEach(field -> {
if (jsonObject.has(field)) {
try {
Object tokenId = jsonObject.opt(field);
String maskedData = tokenizationServiceForMaskedData(tokenId.toString());
jsonObject.put(field, maskedData);
writeWrapper.addHeader(field ".tokenId", tokenId.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
writeWrapper.getOutputStream().write(jsonObject.toString().getBytes());
} catch (JSONException e1) {
e1.printStackTrace();
}
}
private String tokenizationServiceForMaskedData(String tokenId) {
return "****-****-****-*****";
}
}
CodePudding user response:
You have extra "[" and "]"
[{"accountId":"1000"....ate":"PENDING"}]
json should start and end with {} Then this will work:
JSONObject jsonObject = new JSONObject(content);
Or you should first read JSONArray and then read the JSONObject as an item from this Array
JSONArray jsonArray = new JSONArray(content);
JSONObject json = jsonArray.getJSONObject(0);// 0 - index of the json item inside JSONArray
