https://blog.csdn.net/qq_42714869/article/details/93052175
springboot项目,若打包成war包,使用外置的tomcat启动
1、需要继承 org.springframework.boot.context.web.SpringBootServletInitializer类
2、然后重写configure(SpringApplicationBuilder application)方法
-
import org.mybatis.spring.annotation.MapperScan;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.boot.builder.SpringApplicationBuilder;
-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
-
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
-
import org.springframework.cache.annotation.EnableCaching;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.core.annotation.Order;
-
import org.springframework.scheduling.annotation.EnableAsync;
-
import org.springframework.web.cors.CorsConfiguration;
-
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
-
import org.springframework.web.filter.CorsFilter;
-
-
import com.upincar.dms.filter.CharacterEncodingFilter;
-
-
/**
-
*
-
* @Description:启动类
-
*/
-
-
-
-
public class DmsApplication extends SpringBootServletInitializer {
-
-
public static void main(String[] args) {
-
try{
-
SpringApplication.run(DmsApplication.class, args);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
}
-
-
-
/**
-
* 如此配置打包后可以war包才可在tomcat下使用
-
*
-
*
-
*/
-
-
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
-
return application.sources(DmsApplication.class);
-
}
-
-
-
/**
-
* 设置全局UTF-8编码过滤器
-
*
-
*/
-
-
-
public FilterRegistrationBean<CharacterEncodingFilter> characterFilterRegistration() {
-
FilterRegistrationBean<CharacterEncodingFilter> registration = new FilterRegistrationBean<CharacterEncodingFilter>();
-
registration.setFilter(new CharacterEncodingFilter());
-
registration.addUrlPatterns("/*");
-
registration.addInitParameter("paramName", "paramValue");
-
registration.setName("characterEncodingFilter");
-
return registration;
-
}
-
-
-
/**
-
* 跨域过滤器
-
*
-
* @return
-
*/
-
-
public CorsFilter corsFilter() {
-
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
-
source.registerCorsConfiguration("/**", buildConfig());
-
return new CorsFilter(source);
-
}
-
-
private CorsConfiguration buildConfig() {
-
CorsConfiguration corsConfiguration = new CorsConfiguration();
-
corsConfiguration.addAllowedOrigin("*");
-
corsConfiguration.addAllowedHeader("*");
-
corsConfiguration.addAllowedMethod("*");
-
corsConfiguration.setMaxAge(3600L);
-
corsConfiguration.addExposedHeader("x-requested-with");
-
return corsConfiguration;
-
}
-
-
}