@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfig {
@Value("${spring.mvc.servlet.path}")
private String mvcServletPath;
@Bean
@ConditionalOnProperty(prefix = "app", name = "root-enabled", havingValue = "true", matchIfMissing = true)
public ServletRegistrationBean rootServletRegistrationBean(AppProperties appProperties){
String[] baseUrlMappings;
String[] urlMappings;
if(!"/".equals(Paths.get(mvcServletPath).getParent())){
baseUrlMappings = new String[]{Paths.get(mvcServletPath).getParent() + "/", "/"};
urlMappings = new String[]{Paths.get(mvcServletPath) + "/", "/"};
} else {
baseUrlMappings = new String[]{"/"};
urlMappings = new String[]{"/"};
}
ServletRegistrationBean<RootServlet> bean = new ServletRegistrationBean<>(new RootServlet(appProperties, urlMappings), baseUrlMappings);
// 保险起见,把顺序放到最低,确保所有精确匹配先选
bean.setOrder(Ordered.LOWEST_PRECEDENCE);
return bean;
}
@Bean
@ConditionalOnProperty(prefix = "app", name = "describe-enabled", havingValue = "true", matchIfMissing = true)
public ServletRegistrationBean describeServletRegistrationBean(){
return new ServletRegistrationBean<>(new DescribeServlet(), "/describe");
}
}