EnvirWebMvcConfiguration.kt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package cn.gygxzc.envir.web.config
  2. import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
  3. import org.springframework.context.annotation.Bean
  4. import org.springframework.context.annotation.Configuration
  5. import org.springframework.context.annotation.Primary
  6. import org.springframework.http.converter.HttpMessageConverter
  7. import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
  8. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
  9. import org.springframework.web.servlet.config.annotation.EnableWebMvc
  10. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
  11. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
  12. import java.text.SimpleDateFormat
  13. /**
  14. * Created by niantuo on 2018/9/20.
  15. *
  16. */
  17. @Configuration
  18. class EnvirWebMvcConfiguration : WebMvcConfigurationSupport() {
  19. companion object {
  20. const val CONVERTER_NAME = "hookMappingJackson2HttpMessageConverter"
  21. }
  22. override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
  23. registry.addResourceHandler("swagger-ui.html")
  24. .addResourceLocations("classpath:/META-INF/resources/")
  25. registry.addResourceHandler("/webjars*")
  26. .addResourceLocations("classpath:/META-INF/resources/webjars/")
  27. }
  28. override fun configureMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
  29. converters.add(getConverter())
  30. }
  31. @Bean(CONVERTER_NAME)
  32. fun getConverter(): MappingJackson2HttpMessageConverter {
  33. return MappingJackson2HttpMessageConverter().apply {
  34. this.objectMapper = builder().build()
  35. }
  36. }
  37. @Bean
  38. @Primary
  39. fun builder(): Jackson2ObjectMapperBuilder {
  40. return Jackson2ObjectMapperBuilder().apply {
  41. serializerByType(java.lang.Long::class.java, ToStringSerializer.instance)
  42. serializerByType(java.lang.Long.TYPE, ToStringSerializer.instance)
  43. // date
  44. dateFormat(SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
  45. }
  46. }
  47. }