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

TNG Q&A”

Which One To Choose From Python vs Ruby Programming

Completing the bonus task in Stargaze (running IBC Relayer on the server)

Kubernetes Best Practices — Part 2

Why Are You Ahead As CS Graduate

What is the biggest non-tech lesson you have learned on your journey as a software developer?

Web Scraping Instagram with Selenium Python

GraphQL with gqlgen

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

JDBC | MySQL Connection

java mysql database connection

What is Eclipse MicroProfile?

The Pageable object of Spring Data JPA implements paging of the queried list

Java Consultant Tip: SSL Certificates and Man in the Middle SSL Proxy