Add global data format
This commit is contained in:
45
src/main/kotlin/top/fatweb/api/config/DataFormatConfig.kt
Normal file
45
src/main/kotlin/top/fatweb/api/config/DataFormatConfig.kt
Normal file
@@ -0,0 +1,45 @@
|
||||
package top.fatweb.api.config
|
||||
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
|
||||
import org.springframework.boot.jackson.JsonComponent
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
|
||||
import java.text.DateFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.*
|
||||
|
||||
|
||||
@JsonComponent
|
||||
class DataFormatConfig {
|
||||
@set:Value("\${spring.jackson.date-format}")
|
||||
lateinit var dataFormat: String
|
||||
|
||||
@set:Value("\${spring.jackson.time-zone}}")
|
||||
lateinit var timeZone: TimeZone
|
||||
|
||||
@Bean
|
||||
fun jackson2ObjectMapperBuilder() = Jackson2ObjectMapperBuilderCustomizer { builder: Jackson2ObjectMapperBuilder ->
|
||||
val tz = timeZone
|
||||
val df: DateFormat = SimpleDateFormat(dataFormat)
|
||||
df.timeZone = tz
|
||||
builder.failOnEmptyBeans(false)
|
||||
.failOnUnknownProperties(false)
|
||||
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||
.dateFormat(df)
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
fun jackson2ObjectMapperBuilderCustomizer() =
|
||||
Jackson2ObjectMapperBuilderCustomizer { builder: Jackson2ObjectMapperBuilder ->
|
||||
builder.serializerByType(
|
||||
LocalDateTime::class.java, LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dataFormat))
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,9 @@ spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
|
||||
time-zone: GMT
|
||||
|
||||
mybatis-plus:
|
||||
global-config:
|
||||
|
||||
Reference in New Issue
Block a user