package pwc.taxtech.atms; import java.util.TimeZone; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.util.StdDateFormat; @Configuration public class WebMvcConfig implements InitializingBean { private static Logger logger = LoggerFactory.getLogger(WebMvcConfig.class); @Override public void afterPropertiesSet() throws Exception { logger.debug("WebMvcConfig init"); } // @Bean // public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { // FastJsonHttpMessageConverter bean = new FastJsonHttpMessageConverter(); // FastJsonConfig fastJsonConfig = new FastJsonConfig(); // fastJsonConfig.setDateFormat(CommonConstants.DATE_FORMAT); // fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, // SerializerFeature.WriteDateUseDateFormat); // bean.setFastJsonConfig(fastJsonConfig); // // List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); // supportedMediaTypes.add(MediaType.APPLICATION_JSON); // supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); // supportedMediaTypes.add(MediaType.TEXT_HTML); // supportedMediaTypes.add(MediaType.TEXT_PLAIN); // bean.setSupportedMediaTypes(supportedMediaTypes); // return bean; // } @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter bean = new MappingJackson2HttpMessageConverter(objectMapper()); return bean; } @Bean public ObjectMapper objectMapper() { // SimpleDateFormat sdf = new SimpleDateFormat(CommonConstants.DATE_FORMAT); // sdf.setTimeZone(timeZone); // 使用SimpleDateFormat有线程安全问题 // 改为StdDateFormat TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai"); StdDateFormat sdf = StdDateFormat.instance.withTimeZone(timeZone).withColonInTimeZone(true); Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().dateFormat(sdf).timeZone(timeZone); builder.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES); return builder.build(); } @Bean public ResourceBundleMessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); return messageSource; } /** * To use Apache Commons FileUpload, configure a bean of type * CommonsMultipartResolver with the name multipartResolver. Of course you also * need to have commons-fileupload as a dependency on your classpath. */ @Bean(name = "multipartResolver") public CommonsMultipartResolver multipartResolver() { return new CommonsMultipartResolver(); } @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory) { return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000);//单位为ms factory.setConnectTimeout(5000);//单位为ms return factory; } }