配置Spring Boot servlet上下文和过滤器

x33g5p2x  于2022-09-17 转载在 Spring  
字(3.6k)|赞(0)|评价(0)|浏览(422)

在你接受了java config和spring boot之后,下一步就是将你的web.xml转换为编程配置。几乎可以肯定的是,你需要添加一个额外的过滤器,但也有可能出现需要将额外的servletmappings路由到dispatch servlet的情况。迁移的一个小窍门是通过每次剥离一个元素或一层来逐步进行。

在高层次上,当spring boot启动时,它将进入AbstractDispatcherServletInitializer,它将注册dispatch servlet并注册过滤器。有几种方法可以配置servlet映射和过滤器,我们将在下面进行讨论。

###配置额外的servlet映射

如果上下文中只有一个servlet,Spring Boot默认会将dispatcherServlet映射到"/"。在这个例子中,让我们创造一个人为的需求,即基础设施决定了我们需要处理对dispatcherServlet的多个请求映射。你目前的web.xml可能看起来像。

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/whatever/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/whatever2/*</url-pattern>
</servlet-mapping>

ServletRegistrationBean

ServletRegistrationBean是一个具有Spring Bean友好设计的类,用于在spring boot的servlet 3.0容器中注册servlet。在@Configuration类中,你应该能够用以下配置注册ServletRegistrationBean。这应该可以过度写入'/'请求映射并提供'{context}/whatever/'和'{context}/whatever2/'。

@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
    ServletRegistrationBean registration = new ServletRegistrationBean(
            dispatcherServlet);
    registration.addUrlMappings("/whatever/*", "/whatever2/*");
    return registration;
}

SpringBootServletInitializer

如果你在寻找更多的控制,或者已经有了进入WebApplicationInitializer的钩子,你可以直接将请求映射添加到servlet上下文中。

public class MyApplication implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        servletContext.addServlet("dispatchServlet", ...);

        super.onStartup(servletContext);
    }
}

配置额外的过滤器

一个常用的压缩过滤器是plantej的CompressingFilter,它拥有压缩写入HttpServletResponse的数据的所有智能,如果客户端浏览器支持压缩的话。我不会在这里扯开话题,问为什么你的基础设施不在默认情况下支持压缩,而要把它添加到你所有的应用程序中?无论如何,让我们来看看如何将这个压缩过滤器添加到spring boot的上下文中。

定义一个过滤器Bean

你可以定义一个@Bean,这个过滤器就会自动被接收。

@Bean
public Filter compressingFilter() {
    CompressingFilter compressingFilter = new CompressingFilter();
    return compressingFilter;
}

用FilterRegistrationBean注册

如果你需要比用@Bean注解定义过滤器更多一点的控制,你可以使用FilterRegistrationBean

@Bean
public FilterRegistrationBean filterRegistrationBean () {

    CompressingFilter compressingFilter = new CompressingFilter();

    FilterRegistrationBean registrationBean = new FilterRegistrationBean();

    registrationBean.setFilter(compressingFilter);

    return registrationBean;
}

ServletRegistrationBean

在spring boot中注册过滤器的第三种方法,与上面注册servlet映射的方法类似,如果你有一个指向WebApplicationInitializer的钩子,你可以将过滤器添加到servlet上下文中。

public class MyApplication implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        setActiveProfile();

        servletContext.addFilter(filterName, filterClass);

        super.onStartup(servletContext);
    }
}

使用websphere可能出现的问题

IBM Web Sphere 8.0及以上版本开始支持servlet 3.0,这应该让你有能力在不指定web.xml的情况下配置你的应用程序。如果你使用spring-boot、ibm websphere和servlet 3.0,并试图在没有web.xml的情况下进行部署,那么可能会出现问题。这并不是全面确认的,因为每个IBM websphere商店都有一个专门的自定义层在webpshere之上。当部署到websphere 8.5时,他们似乎并不完全支持消除web.xml,这导致部署问题。一个解决方法是创建一个空的web.xml文件,然后设置metadata-complete="false"。

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" metadata-complete="false">
    <display-name>Servlet 3.0 Web Application</display-name>
</web-app>

相关文章