EnvirWebMvcConfiguration.kt 2.2 KB

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