Browse Source

json validation for empty spaces

master
yoganandhank 1 year ago
parent
commit
bbc38adbf6
  1. 1
      src/main/java/com/gcc/crslogs/controller/ApiController.java
  2. 29
      src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java
  3. 4
      src/main/resources/templates/modules/crslogspages/birthanddeath.html

1
src/main/java/com/gcc/crslogs/controller/ApiController.java

@ -1,6 +1,7 @@
package com.gcc.crslogs.controller; package com.gcc.crslogs.controller;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;

29
src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java

@ -36,6 +36,10 @@ public class BirthAndDeathDetailsService {
this.restTemplate = builder.build(); this.restTemplate = builder.build();
} }
public Map<String, Object> fetchBirthCertificate(String dob, String sex, String regno, String childName, String fatherName, String motherName) throws Exception { public Map<String, Object> fetchBirthCertificate(String dob, String sex, String regno, String childName, String fatherName, String motherName) throws Exception {
Map<String, String> params = new LinkedHashMap(); // keep order Map<String, String> params = new LinkedHashMap(); // keep order
params.put("regno", regno != null ? regno : ""); params.put("regno", regno != null ? regno : "");
@ -61,10 +65,14 @@ public class BirthAndDeathDetailsService {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(BirthCertificateapiUrl, HttpMethod.GET, entity, String.class); ResponseEntity<String> response = restTemplate.exchange(BirthCertificateapiUrl, HttpMethod.GET, entity, String.class);
String rawJson = response.getBody();
String cleanJson = sanitizeJsonResponse(rawJson);
//System.out.println("✅ API Raw Response: " + response.getBody());
//System.out.println("✅ API Raw Response: " + cleanJson);
return mapper.readValue(response.getBody(), new TypeReference<Map<String, Object>>() {});
return mapper.readValue(cleanJson, new TypeReference<Map<String, Object>>() {});
} }
@ -101,10 +109,25 @@ public class BirthAndDeathDetailsService {
ResponseEntity<String> response = restTemplate.exchange(DeathCertificateapiUrl, HttpMethod.GET, entity, String.class); ResponseEntity<String> response = restTemplate.exchange(DeathCertificateapiUrl, HttpMethod.GET, entity, String.class);
//System.out.println("✅ API Raw Response: " + response.getBody()); //System.out.println("✅ API Raw Response: " + response.getBody());
String rawJson = response.getBody();
String cleanJson = sanitizeJsonResponse(rawJson);
return mapper.readValue(response.getBody(), new TypeReference<Map<String, Object>>() {});
return mapper.readValue(cleanJson, new TypeReference<Map<String, Object>>() {});
} }
private String sanitizeJsonResponse(String rawJson) {
if (rawJson == null) return "";
// Remove illegal control characters (but keep line breaks if needed)
return rawJson
.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters (except tabs/newlines)
.replaceAll("\t", "") // Remove tab characters explicitly (common cause of error)
.replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits
.replaceAll("\r", "") // Remove carriage return if any
.replaceAll("\n", ""); // Optionally remove newlines if JSON is malformed across lines
}

4
src/main/resources/templates/modules/crslogspages/birthanddeath.html

@ -969,6 +969,7 @@ document.addEventListener("DOMContentLoaded", function () {
function fetchBirthData() { function fetchBirthData() {
const dob = document.getElementById('dateofbirth').value; const dob = document.getElementById('dateofbirth').value;
const gender = document.querySelector('input[name="gender"]:checked')?.value; const gender = document.querySelector('input[name="gender"]:checked')?.value;
const captchaInput = document.getElementById('birth-captcha-input'); const captchaInput = document.getElementById('birth-captcha-input');
@ -1019,7 +1020,10 @@ $.ajax({
success: function (response) { success: function (response) {
$('#pageLoader').hide(); $('#pageLoader').hide();
if (response && response.BIRTH && response.BIRTH.length > 0) { if (response && response.BIRTH && response.BIRTH.length > 0) {
console.log("birthdetails:",response);
let html = ` let html = `
<table id="birthDataTable" class="table table-bordered table-striped"> <table id="birthDataTable" class="table table-bordered table-striped">
<thead> <thead>

Loading…
Cancel
Save