com.feingto.cloud.config.annotation.WebMvcAutoConfiguration Maven / Gradle / Ivy
package com.feingto.cloud.config.annotation;
import com.feingto.cloud.core.json.JSONObjectMapper;
import com.feingto.cloud.core.web.helper.ApplicationContextHelper;
import com.feingto.cloud.core.web.resolver.ExceptionResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* WebMvcAutoConfiguration
*
* @author longfei
*/
@Slf4j
@Configuration
@EnableAsync
public class WebMvcAutoConfiguration implements WebMvcConfigurer {
private MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.setObjectMapper(new JSONObjectMapper().useDateTime());
return jackson2HttpMessageConverter;
}
/**
* Warning: 重写configureMessageConverters会被其他的相同实例覆盖, 改为重写extendMessageConverters几率会很小
*/
@Override
public void extendMessageConverters(List> converters) {
log.debug("Extend messageConverters jackson2HttpMessageConverter.");
converters.clear();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
converters.add(new ResourceHttpMessageConverter());
converters.add(new ResourceRegionHttpMessageConverter());
converters.add(new SourceHttpMessageConverter());
converters.add(new AllEncompassingFormHttpMessageConverter());
converters.add(jackson2HttpMessageConverter());
converters.add(new MappingJackson2XmlHttpMessageConverter());
}
@Override
public void configureHandlerExceptionResolvers(List exceptionResolvers) {
exceptionResolvers.add(new ExceptionResolver());
}
@Bean
public Executor threadPoolTaskExecutor() {
log.debug("Configuring threadPoolTaskExecutor.");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(500);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(3000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("AsyncTaskExecutor-");
// 当pool大道max size时不在新线程中执行,而是在调用者所在的线程中执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@Bean
public ApplicationContextHelper applicationContextHelper() {
return new ApplicationContextHelper();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy