A next step after you have bought into moving to java config and spring boot is tackling converting your web.xml to be configured programmatically. It is almost given that you will need to add an additional filters but there also might be a use case where you need to route additional servletmappings to the dispatch servlet. One migration tip is incrementally make your way by doing peeling one element or layer at at time.
At a high level, when spring boot fires up it will make its way to AbstractDispatcherServletInitializer which will register the dispatch servlet and register filters. There is a couple of ways to configure servlet mapping and filters which we will work through below.
Configuring with additional servlet mappings
Spring boot by default will map the dispatcherServlet to '/' if there is only a single servlet in context. In the example lets create an artificial requirement where infrastructure is dictating we need to handle multiple request mapping to the dispatcherServlet. Your current web.xml might look like:
ServletRegistrationBean
ServletRegistrationBean is class with a spring bean friendly design that is used to register servlets in a servlet 3.0 container within spring boot. Within a @Configuration class, you should be able to register ServletRegistrationBean with the following configuration. This should over write the '/' request mapping and provide '{context}/whatever/' and '{context}/whatever2/'.
SpringBootServletInitializer
If you looking for more control or already have a hook into the WebApplicationInitializer you could add the request mapping directly to the servlet context.
Configuring additional filters
A popular compression filter used is the plantej CompressingFilter which has all the smarts on compressing data written to HttpServletResponse and if the client browser supports compression. I won't go on a tangent here and ask why doesn't your infrastructure just support compression by default vs having to add it to all of your apps?! At any rate, lets look at how we can add this compression filter to spring boot context.
Defining a filter bean
You can define a @Bean and the filter should automatically get picked up.
Register with FilterRegistrationBean
If you need a bit more control than just defining the filter with @Bean annotation, you could use the FilterRegistrationBean
ServletRegistrationBean
A third way to register a filter in spring boot, which is similar to how register a servlet mapping above, is if you have a hook to the WebApplicationInitializer you can add the filter to the servlet context.
Possible gotcha with websphere
IBM Web Sphere 8.0 and above started supporting servlet 3.0 which should give you the ability to configure your application without specifying a web.xml. If you are using spring-boot, ibm websphere, and servlet 3.0 and are trying to deploy without a web.xml there might be a possible gotcha. This isn't confirmed across the board since each IBM websphere shops have a specialized custom layer on top of webpshere. When deploying to websphere 8.5, it doesn't appear that they fully support eliminating the web.xml which causes deployment issues. One workaround is creating an empty web.xml file and then set the metadata-complete="false".