SpringBoot 添加多个servlet

springboot 添加多个servlet

参考问题链接

除了网上可以搜到的几种方法外,还可以实现ServletContextInitializer接口(spring下实现WebApplicationInitializer接口,springboot 内嵌tomcat时实现WebApplicationInitializer无效)

将view和api分开

View-DispatcherServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @author techoneduan
* @date 2019/1/2
*/
@Configuration
public class WebInitializer implements ServletContextInitializer {
@Override
public void onStartup (ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext dispatcherCtx = new AnnotationConfigWebApplicationContext();
dispatcherCtx.register(WebConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("View-dispatcher", new DispatcherServlet(dispatcherCtx));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);

//字符集过滤器
FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class);
characterEncodingFilter.setInitParameter("encoding", "UTF-8");
characterEncodingFilter.setInitParameter("forceEncoding", "true");
characterEncodingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
}

Api-DispatcherServlet
可以添加一些自定义的过滤器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @author techoneduan
* @date 2018/12/6
*/
@Configuration
public class ApiWebInitiallizer implements ServletContextInitializer {
@Override
public void onStartup (ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext apiDispatcherCtx = new AnnotationConfigWebApplicationContext();
apiDispatcherCtx.register(ApiWebConfig.class);
ServletRegistration.Dynamic apiDispatcher = servletContext.addServlet("web-api-dispatcher", new DispatcherServlet(apiDispatcherCtx));
apiDispatcher.addMapping("/api/*");
apiDispatcher.setLoadOnStartup(1);

FilterRegistration.Dynamic base64DecodingFilter = servletContext.addFilter("base64DecodingFilter",Base64DecodingFilter.class);
base64DecodingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),false,"/api/*");

FilterRegistration.Dynamic framefilter = servletContext.addFilter("frameworkFilter",FrameWorkFilter.class);
framefilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),false,"/api/*");
}
}

config类

@ComponentScan注解必须配合@EnableWebMvc使用

ViewWebConfig
配置视图解析器、静态资源处理

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
/**
* @author techoneduan
* @date 2019/1/2
*/
@Configuration
@ComponentScan(basePackages = "com.eureka.client.web.view")
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

@Bean
public InternalResourceViewResolver viewResolver () {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
// viewResolver.setViewClass(JstlView.class);
return viewResolver;
}

// @Bean
// public DispatcherServlet webDispatcherServlet () {
// AnnotationConfigWebApplicationContext ctx
// = new AnnotationConfigWebApplicationContext();
// ctx.register(WebConfig.class);
// DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
// return dispatcherServlet;
// }
//
// @Bean
// public ServletRegistrationBean servletRegistration () {
// ServletRegistrationBean registration = new ServletRegistrationBean(webDispatcherServlet(), "/");
// registration.setName("client-dispatcher");
// registration.setLoadOnStartup(1);
// return registration;
// }

/**
* 静态资源配置
*
* @param registry
*/
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}

@Override
public void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

}

ApiWebConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @author techoneduan
* @date 2018/12/28
*/
@Configuration
@ComponentScan(basePackages = "com.eureka.client.web.api")
@EnableWebMvc
public class ApiWebConfig implements WebMvcConfigurer {

@Bean
public JsonMapperArgumentResolver jsonMapperArgumentResolver () {
JsonMapperArgumentResolver jsonMapperArgumentResolver = new JsonMapperArgumentResolver();
return jsonMapperArgumentResolver;
}

@Override
public void addArgumentResolvers (List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(jsonMapperArgumentResolver());

}

}
Thanks!