1. When data of type localdate
in Java is tested on swagger, the format when input in the format of json
is 2018-07-09
. It should be noted that 07 and 09 are two digits, not one digit.
2. If the date is of LocalDate type, JacksonAutoConfiguration will automatically process whether the foreground transmits the date in string format to the background or the background returns the date in format to the front end.
3. If the date is of LocalDateTime type, we need to process it from the front end to the back end and from the back end to the front end. Because the configuration in the following YML does not apply to Java 8 date types, such as LocalDate and LocalDateTime, it only applies to fields of date or DateTime type.
#Date Formatting
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
Solution: add LocalDateTimeConfig configuration class
/**
* LocalDateTime
*/
@Configuration
public class LocalDateTimeGlobalConfig {
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
*Configuring LocalDateTime type serialization and deserialization
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
/*return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
jacksonObjectMapperBuilder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
}
};*/
//This approach is equivalent to the above
return builder -> {
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
};
}
}
Read More:
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- [Solved] JSON parse error: Cannot deserialize instance of `java.util.ArrayList<..> out of START_OBJECT token;
- [Solved] Springboot uses redis to add LocaldateTime Java 8 error
- Java back end receives localdatetime type parameter
- The problem of date format conversion between springboot and front end
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- [Solved] Openfeign Error: error: Failed to parse Date value…
- [Solved] HttpMessageNotReadableException: JSON parse error: Unrecognized field “xxxx“
- How to get the current time in java time string
- org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deseria
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class
- [Solved] org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot
- [Solved] JSON parse error: Cannot construct instance of
- How to convert a Java string into a number (stringtonumber)
- JSON parse error: invalid UTF-8 solution series [How to Solve]
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- [Solved] Feign Call Port Error: HttpMessageNotReadableException:JSON parse error:Illegal character ((CTRL-CHAR, code 31)
- [Solved] Error attempting to get column ‘xxxx_time‘ from result set. Cause: java.sql.SQLFeatureNotSupportedEx
- JAVA: How to Read JSON Format Data (Web Game Development)
- JAVA 8: How to Convert List to Map