How to parse REST API json data comprising of uppercase attribute name, Date and Instant in a java spring application ?

{
"FIRST_NAME" : "Rohan",
"LAST_NAME":"KUMAR",
"DATE_OF_BIRTH": "20-SEP-1990",
"UPLOAD_DATE": "2021-05-31T06:13:23.000Z"
}
public class StudentDTO{
private String FIRST_NAME;
private String LAST_NAME;
private LocalDate DATE_OF_BIRTH;
privet Instant UPLOAD_DATE;
}
public StudentDTO getStudent(){
RestTemplate restTemplate = restTemplate();
String url= “http://localhost:9000/student/1";
StudentDTO student = restTemplate.getForObject(url, StudentDTO.class);
return student;
}
  1. Spring does not support uppercase attribute name. So despite having the attribute name “FIRST_NAME”, it gets deserialized as “FIRST_name” as json.
  2. Java Date formatter doe not supports the syntax “20-SEP-1990”. It however supports the syntax “20-Sep-1990”.
  3. As the timezone was not explicitly mentioned, parsing of Instant data “2021–05–31T06:13:23.000Z” was taking a very long time.
public class StudentDTO{@JsonProperty("FIRST_NAME")
private String firstName;
@JsonProperty("LAST_NAME")
private String lastName;
@JsonProperty("DATE_OF_BIRTH")
@JsonDeserialize(using = LocalDateDeserializer.class) // Use custom deserializer class
private LocalDate dateOfBirth;
@JsonProperty("UPLOAD_DATE")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "UTC")
private Instant uploadDate;
}
public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException {
String strDate= jsonparser.getText();
//Convert month name from upper case to Titel case. eg. SEP to Sep
String modDate = strDate.substring(0,4)+strDate.substring(4,6).toLowerCase()+strDate.substring(6);
return LocalDate.parse(modDate, DateTimeFormatter.ofPattern("dd-MMM-yyyy"));
}
}
public StudentDTO getStudent(){RestTemplate restTemplate = restTemplate();
String url= “http://localhost:9000/student/1";
StudentDTO student = restTemplate.getForObject(url, StudentDTO.class);
return student;
}

--

--

--

System Administrator and Full stack web developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Law Students Starter Pack: “How to Ace a Legal Opinion Competition.”

Online Grocery System your All kind of Grocer Shops

Best Grocery Deivery App

On dataset versioning in Palantir Foundry

What Exactly Is Native Mobile Application Development?

PHP vs Javascript Syntax

https://t.co/G6umPOEqwP

Test-ready development

Hello World. I ‘ve become a Software Developer!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Himanshu Pratap

Himanshu Pratap

System Administrator and Full stack web developer.

More from Medium

Static Code Analysis

Automating tests with Squash DEVOPS

How to Use Microservices Using Simple Queue Service

Effective Session Management using Supertokens