其他分享
首页 > 其他分享> > 在Spring Boot中为Servlet配置添加过滤器

在Spring Boot中为Servlet配置添加过滤器

作者:互联网

我使用spring-boot-starter-web包来构建基于spring-batch-admin的webapp.

@Configuration
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class, DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class })
@Import(MainConfiguration.class)
@EnableTransactionManagement
public class BatchAdmin extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(BatchAdmin.class, args);
  }
  // ...
}      

@Configuration
@ComponentScan("com.company.package*")
@Import({ ServletConfiguration.class, WebappConfiguration.class })
public class MainConfiguration {}

@Configuration
@ImportResource("classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml")
public class ServletConfiguration {}

@Configuration
@ImportResource({ "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml","classpath:persistence-context.xml" })
public class WebappConfiguration {}

如何使用java配置样式将此过滤器添加到servlet上下文?

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

解决方法:

有时(但不经常)查询文档比询问SO社区更快.

根据官方spring-boot documentation,我不得不将以下bean定义添加到BatchAdmin类.

@Bean
public Filter hiddenHttpMethodFilter() {
   HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
   return filter;
}

标签:spring-mvc,spring,spring-boot-2,spring-batch-admin
来源: https://codeday.me/bug/20190624/1277423.html