[BAEL-2533] Formatting JSON Dates in Spring Boot (#6313)
* [BAEL-2533] formatting json date output in spring boot * [BAEL-2533] removed unused import
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Contact {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd")
|
||||
private LocalDate birthday;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public LocalDate getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(LocalDate birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public LocalDateTime getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(LocalDateTime lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public Contact() {
|
||||
}
|
||||
|
||||
public Contact(String name, String address, String phone, LocalDate birthday, LocalDateTime lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ContactApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ContactApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Configuration
|
||||
public class ContactAppConfig {
|
||||
|
||||
private static final String dateFormat = "yyyy-MM-dd";
|
||||
|
||||
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "spring.jackson.date-format", matchIfMissing = true, havingValue = "none")
|
||||
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
|
||||
return new Jackson2ObjectMapperBuilderCustomizer() {
|
||||
@Override
|
||||
public void customize(Jackson2ObjectMapperBuilder builder) {
|
||||
builder.simpleDateFormat(dateTimeFormat);
|
||||
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
|
||||
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/contacts")
|
||||
public class ContactController {
|
||||
|
||||
@GetMapping
|
||||
public List<Contact> getContacts() {
|
||||
List<Contact> contacts = new ArrayList<>();
|
||||
|
||||
Contact contact1 = new Contact("John Doe", "123 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
Contact contact2 = new Contact("John Doe 2", "124 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
Contact contact3 = new Contact("John Doe 3", "125 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@GetMapping("/javaUtilDate")
|
||||
public List<ContactWithJavaUtilDate> getContactsWithJavaUtilDate() {
|
||||
List<ContactWithJavaUtilDate> contacts = new ArrayList<>();
|
||||
|
||||
ContactWithJavaUtilDate contact1 = new ContactWithJavaUtilDate("John Doe", "123 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
ContactWithJavaUtilDate contact2 = new ContactWithJavaUtilDate("John Doe 2", "124 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
ContactWithJavaUtilDate contact3 = new ContactWithJavaUtilDate("John Doe 3", "125 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@GetMapping("/plain")
|
||||
public List<PlainContact> getPlainContacts() {
|
||||
List<PlainContact> contacts = new ArrayList<>();
|
||||
|
||||
PlainContact contact1 = new PlainContact("John Doe", "123 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
PlainContact contact2 = new PlainContact("John Doe 2", "124 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
PlainContact contact3 = new PlainContact("John Doe 3", "125 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@GetMapping("/plainWithJavaUtilDate")
|
||||
public List<PlainContactWithJavaUtilDate> getPlainContactsWithJavaUtilDate() {
|
||||
List<PlainContactWithJavaUtilDate> contacts = new ArrayList<>();
|
||||
|
||||
PlainContactWithJavaUtilDate contact1 = new PlainContactWithJavaUtilDate("John Doe", "123 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
PlainContactWithJavaUtilDate contact2 = new PlainContactWithJavaUtilDate("John Doe 2", "124 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
PlainContactWithJavaUtilDate contact3 = new PlainContactWithJavaUtilDate("John Doe 3", "125 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class ContactWithJavaUtilDate {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd")
|
||||
private Date birthday;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public Date getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(Date lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public ContactWithJavaUtilDate() {
|
||||
}
|
||||
|
||||
public ContactWithJavaUtilDate(String name, String address, String phone, Date birthday, Date lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class PlainContact {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
private LocalDate birthday;
|
||||
|
||||
private LocalDateTime lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public LocalDate getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(LocalDate birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public LocalDateTime getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(LocalDateTime lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public PlainContact() {
|
||||
}
|
||||
|
||||
public PlainContact(String name, String address, String phone, LocalDate birthday, LocalDateTime lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class PlainContactWithJavaUtilDate {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd")
|
||||
private Date birthday;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public Date getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(Date lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public PlainContactWithJavaUtilDate() {
|
||||
}
|
||||
|
||||
public PlainContactWithJavaUtilDate(String name, String address, String phone, Date birthday, Date lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user