Is it possible to use DispatcherServlet as fallback when using Jersey starter in Spring Boot and embedded Tomcat? - spring-boot

Is it possible to have Jersey application on / but still allow DispatcherServlet to serve static content (eg from classpath:/whatever/path/here/) as a fallback?
I have tried to map DispatcherServlet to / and use JersetContainer (or something like that) to act as Filter instead of Servlet, however Jersey fallsback to default container servlet implementation which is DefaultServlet in my case.
So is it possible to have Jersey as a filter on the same mapping path with DispatcherServlet ??

Related

What is difference between Spring Dispatcher Servlet and Web Deployment Descriptor?

Why do need to create a separate Spring-servlet.xml(Dispatcher Servlet) file in my Spring application? Why we can not keep all the request mapping in web.xml(Deployment Descriptor file)?
J2EE specification drives what content has to be available inside web.xml. Web.xml provides URL to Servlet mapping where as Spring dispatcher servlet with help of its own configuration file provides mapping of request URL with controller class.

Can I use Jersey as both servlet and filter in Spring Boot?

According to Spring documentation:
You can also use a Filter instead of a Servlet by setting spring.jersey.type=filter (in which case the #Bean to replace or override is jerseyFilterRegistration).
According to the answers to this question:
Use a Filter when you want to filter and/or modify requests based on specific conditions. Use a Servlet when you want to control, preprocess and/or postprocess requests.
So what if want to use Jersey with Spring Boot, to both serve json content and apply filters such as checking for authorisation and adding headers to all responses? The spring documentation reads like I have to choose either filter or servlet role for Jersey.
Can I do both with Jersey in a Spring Boot application and if so, how ?
So what if want to use Jersey with Spring Boot, to both serve json content and apply filters such as checking for authorisation and adding headers to all responses?
This question doesn't really make much sense.
Jersey itself is a processing engine. All it needs from the servlet container is the HttpServeltRequest and HttpServletResponse to start processing the request. These can be obtained both as a servlet Filter, or as a servlet HttpServlet. And if you look at the main Jersey servlet container component, ServletContainer, you will see that it both extends HttpServlet and implements Filter.
So being able to configure Jersey as a filter or as a servlet is not anything specific to Spring Boot; Jersey is designed this way. You could configure Jersey as a filter or servlet without Spring Boot.
As far as the filter system, Jersey has it's own filter system, independent of any servlet APIs. But if you want to use servlet filters, there's no reason you can't, whether or not you configure Jersey as a filter or as a servlet. If you understand the servlet filter chain, then you will know that filters get called one after the next, then the servlets are called. So if you want to add a filter and have it perform before the Jersey filter, you can do that. Or if Jersey is a servlet, your filter will be called before the Jersey servlet. Either way it is the same result. Jersey doesn't change any processing behavior just because it is a filter or if is a servlet.
The spring documentation reads like I have to choose either filter or servlet role for Jersey
Yes Servlet or Filter. Should be clear from what I mentioned above. You can have more than one servlet filter. Filters happen one after the other. You can add a filter that has nothing to do with Jersey. They all get passed the same ServletRequest and ServletResponse, so they all interacting with the same request and response. If you want to create a filter to add headers, then do it. It doesn't need to know anything about Jersey.
If you want to create Jersey specific filters (which is independent of any servlet filter mechanisms) for auth/headers and such, you can look at Filters and Interceptors (you can see an auth example in this great answer).
If you want to add servlet filters, then you can do so with FilterRegistrationBeans in Spring Boot.
#Bean
public FilterRegistrationBean anotherFilter() {
}

Servlet/Filters/Listeners in Spring Boot Application

I've added Servlet/Filters/Listeners in Spring Boot application using ServletRegistrationBean , FilterRegistrationBean etc.. for that we have to declare servlets, filters as spring bean ..which will get added in Spring Application Context..which is working absolutely fine
My Question is ..whenever i will call my application, will the request FIRST be handled by Dispatcher Servlet and then to Filter?
Before spring boot, we use to register it directly in web.xml and then i think Filters used to handler the request first, then dispatcher servlet and so on and so forth..
Has the flow changed in Spring Boot Application?

Spring Boot on WildFly (or Undertow): Servlet Filter's orders are reversed

I deployed my Spring Boot Web application on WildFly 8.1, but now I have some trouble.
When application was deployed, FilterRegistrationBeans register Servlet Filters. I know FilterRegistrationBeans are oredered by AnnotationAwareOrderComparator, and those filters are registerd by that order. But when I access my application, Undertow calls filters by reversed order.
For example, if Spring Boot register filters like that:
errorPageFilter
metricFilter (from Spring Boot actuator)
characterEncodingFilter
hiddenHttpMethodFilter
springSecurityFilterChain (from Spring Security)
Undertow call those filters like that:
springSecurityFilterChain
hiddenHttpMethodFilter
characterEncodingFilter
metricFilter
errorPageFilter
How can I specify those filters order correctly? Some filter's order (like org.springframework.boot.context.web.ErrorPageFilter) was hard-coded in source, I can't specify that.
This appears to be an Undertow bug that occurs when matchAfter is false in FilterRegistrationImpl#addMappingForUrlPatterns, instead of inserting the filter before all declared filters it inserts it before all filters.

How can I pass certain properties to the Spring context through servlet params?

So we decided to deploy our application no more as a war on the usual tomcat, but from an embedded jetty. My app uses context:property-placeholder to resolve properties from a file. Now I'd need to pass some (or all) properties programatically before starting jetty. Is there a way that allows me to set some properties by code, before running jetty, instead of relying on the .properties file? For example as Servlet params?
You can use ServletContextPropertyPlaceholderConfigurer. This PropertyPlaceholderConfigurer extract the properties from the servlet context init params.
From Spring Javadocs:
Subclass of PropertyPlaceholderConfigurer that resolves placeholders as ServletContext init parameters (that is, web.xml context-param entries).
But this class is deprecated from Spring version 3.1.
From version 3.1 you don't need to use any special configuration because all Web Based servlet context use the class org.springframework.web.context.support.StandardServletEnvironment that resolve properties from servlet context params by default.

Resources