package cn.gygxzc.envir.config import com.fasterxml.jackson.databind.ser.std.ToStringSerializer import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary import org.springframework.http.converter.HttpMessageConverter import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer import java.text.SimpleDateFormat /** * Created by niantuo on 2018/9/20. * mvc 配置 * */ @Configuration open class EnvirWebMvcConfiguration : WebMvcConfigurer { companion object { const val CONVERTER_NAME = "hookMappingJackson2HttpMessageConverter" } override fun addResourceHandlers(registry: ResourceHandlerRegistry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/") registry.addResourceHandler("/webjars*") .addResourceLocations("classpath:/META-INF/resources/webjars/") } override fun configureMessageConverters(converters: MutableList>) { converters.add(getConverter()) } @Bean(CONVERTER_NAME) open fun getConverter(): MappingJackson2HttpMessageConverter { return MappingJackson2HttpMessageConverter().apply { this.objectMapper = builder().build() } } @Bean @Primary open fun builder(): Jackson2ObjectMapperBuilder { return Jackson2ObjectMapperBuilder().apply { serializerByType(java.lang.Long::class.java, ToStringSerializer.instance) serializerByType(java.lang.Long.TYPE, ToStringSerializer.instance) // date dateFormat(SimpleDateFormat("yyyy-MM-dd HH:mm:ss")) timeZone("GMT+8") } } }