WebMvcConfig.java 3.4 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
package pwc.taxtech.atms.web;

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.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
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("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//        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();
    }

}