NotFound
千里之行始于足下
Spring Boot 添加 servlet 以及 filter

Spring Boot 添加 servlet 以及 filter

  • OpenJDK 11
  • Gradle 6.8

依赖

implementation 'org.springframework.boot:spring-boot-starter-web'

servlet

ServletWrappingController

@Bean
public ServletWrappingController wrappingController() {
    ServletWrappingController controller = new ServletWrappingController();
    controller.setServletClass(WrappingServlet.class);
    controller.setServletName("wrapping-servlet");
    return controller;
}

@Bean
public SimpleUrlHandlerMapping urlHandlerMapping() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setUrlMap(Map.of("/wrapping/*", "wrappingController"));
    mapping.setOrder(Ordered.LOWEST_PRECEDENCE  - 2);
    return mapping;
}
  • req.getPathInfo()null

ServletRegistrationBean

@Bean
public ServletRegistrationBean<HelloServlet> helloServletServletRegistrationBean() {
    ServletRegistrationBean<HelloServlet> registration = new ServletRegistrationBean<>();
    registration.setServlet(new HelloServlet());
    registration.setUrlMappings(List.of("/hello/*"));
    return registration;
}
  • req.getPathInfo()* 的内容

filter

@Bean
public FilterRegistrationBean<HelloFilter> helloFilterFilterRegistrationBean() {
    FilterRegistrationBean<HelloFilter> registration = new FilterRegistrationBean<>();
    registration.setFilter(new HelloFilter());
    return registration;
}

参考


Last modified on 2021-04-14