Java Date Time – How to build SpringBoot RestApi – Post/Get request with Java Date Time using Jackson and Make Query with Spring JPA example

spring-boot-rest-api-date-time-mysql-post-json-form-feature-image


In the tutorial, we build a SpringBoot RestAPIs example that post/get data with java.util.Date time and save it to MySQL/PostgreSQL database using Spring JPA. Working with Java Date Time is an exciting part but also not easy task, fortunately we have the supporting from utilities of Jackson lib, now the job can be done in an easy way.

Let’s do details by steps!

Format Java Date Time with Jackson

Set the Format with @JsonFormat

With the @JsonFormat annotation of Jackson, we can use it to format a specific field in Java model:

Set TimeZone with @JsonFormat

For setting Time Zone, we use timezone attribute of the @JsonFormat:

Set Default Format

We can configure a default format & time-zone for all dates in application.properties:

Map Date and Time with Spring JPA

We use the @Temporal annotation to specify what the field represents. This annotation goes with a parameter having a value of TemporalType enum:

-> Now, our model will be like:

Query the Entities with Date Time property from Database

For query data with Date Time property from database, we need to implement CrudRepository and use 2 approaches.

– Use the built-in rule of Spring JPA mechanics:

– Use @Query annotation:

RequestParam with Date Time in RestApi

For adding a request param with Date Time data type in RestAPI, we use @RequestParam & @DateTimeFormat

Practice

Create SpringBoot Project

We create a SpringBoot project as below structure:

spring-boot-rest-api-date-time-mysql-post-json-form-project-structure

– Dependencies List:

Create Dates & Times Model

– Create DateTimeModel.java model:

Create Repository

– Create DateTimeRepository.java repository:

Create RestAPIs POST/GET/QUERY

– Create DateTimeController.java restapis:

Database Configuration

Config MySQL

application.properties:

Config PostgreSQL

application.properties:

Run & Check Result

Run SpringBoot project, then makes requests.

– Post request:

spring-boot-rest-api-date-time-mysql-post-json-form

spring-boot-rest-api-date-time-mysql-post-json-form-records

spring-boot-rest-api-date-time-mysql-post-json-form-records-all-records

– Get All Entities request:

spring-boot-rest-api-date-time-mysql-post-json-form-get-all-request

– Query Request ->

+ Get Entities between 2 Date Time:

spring-boot-rest-api-date-time-mysql-post-json-form-get-date-time-between

+ Get Entities before a Date Time:

spring-boot-rest-api-date-time-mysql-post-json-form-get-all-days-before

Sourcecode

SpringBootRestApiDateTimeMySQL

Conclusion

Through the tutorial, We had learned how to create a SpringBoot RestAPI with Java Date Time data:

  • Using @JsonFormat annotation to format Date Time
  • Using @Temporal annotation to map Date and Time to work with Spring JPA
  • Query the Entities with Date Time property from Database
  • Format RequestParam with Date Time type in RestAPI by using @DateTimeFormat

Thank you for reading! See you next time!

By grokonez | March 25, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*