com.feingto.cloud.config.web.WebMvcAutoConfiguration Maven / Gradle / Ivy
The newest version!
package com.feingto.cloud.config.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.feingto.cloud.config.annotation.ExcludeComponentScan;
import com.feingto.cloud.kit.json.JSONObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* WebMvcAutoConfiguration
*
* @author longfei
*/
@Configuration(proxyBeanMethods = false)
//@EnableAsync
@ExcludeComponentScan
public class WebMvcAutoConfiguration implements WebMvcConfigurer {
private final ObjectMapper objectMapper = new JSONObjectMapper().useDateTime();
/**
* 扩展消息转换器
*
* @param converters the list of configured converters to extend
*/
@Override
public void extendMessageConverters(List> converters) {
converters.stream()
.filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
.map(MappingJackson2HttpMessageConverter.class::cast)
.peek(converter -> converter.setObjectMapper(objectMapper))
.findAny()
.orElseGet(() -> {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
converters.add(converter);
return converter;
});
}
/**
* 异常解析器
*
* @param resolvers initially an empty list
*/
@Override
public void configureHandlerExceptionResolvers(List resolvers) {
resolvers.add(new ExceptionHandlerExceptionResolver());
resolvers.add(new ResponseStatusExceptionResolver());
resolvers.add(new DefaultHandlerExceptionResolver());
}
/**
* Thread Pool Executor
*/
@Bean
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(500);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(3000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("AsyncTaskExecutor-");
// 线程池超过阈值时不开启新线程而是在调用者当前线程中执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@LoadBalanced
@Bean
@ConditionalOnMissingBean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new RestResponseErrorHandler());
return restTemplate;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy