I am trying to add webflow and security configuration to an operational Spring 3.1 MVC web application.
I got rid of the application-context.xml file and ContextLoaderListener, and use AnnotationConfigWebApplicationContext with an #EnableWebMvc #Configuration annotated class.
Yet, when I follow the webflow and security documentation, it seems like an application-context.xml file and ContextLoaderListener are necessary. The later seems to create a conflict with AnnotationConfigWebApplicationContext.
Can anyone provide a simple but complete Spring 3.1 MVC Web Application configuration with webflow and security enabled example? I am looking for a web.xml example file, plus any other required files.
If someone has a complete sample application downloadable online, it is even better.
The following works when deploying to Tomcat 7:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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"
metadata-complete="false">
<!-- Context Params -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.mypackage.web.WebConfig</param-value>
</context-param>
<!-- Filters -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declaring and configuring the default Spring Servlet -->
<servlet>
<servlet-name>springExample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Enabling annotation configuration for web app -->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Loading order -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springExample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
WebConfig
#EnableWebMvc
#ImportResource({ "/WEB-INF/spring-security.xml", "/WEB-INF/spring-webflow.xml"})
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
// ...
}
Caveat: using #Import(SomeConfig.class) does not work anymore. It causes initialization issues. The workaround is to move the content of SomeConfig.class to WebConfig.class itself.
It is important to add proper maven dependencies, otherwise, Tomcat results in 404 Resource not found error messages:
pom.xml
<properties>
...
<spring.framework.version>3.1.2.RELEASE</spring.framework.version>
<spring.security.version>3.1.1.RELEASE</spring.security.version>
<spring.webflow.version>2.3.1.RELEASE</spring.webflow.version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.framework.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>${spring.webflow.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-binding</artifactId>
<version>${spring.webflow.version}</version>
</dependency>
</dependencies>
Related
I have been working on my own REST API built with jersey and java. Today it stopped working though after a friend who also works on it pushed his changes. he didn't change anything on the dependency side, but he did add an controller that our main API class creates. Now whenever I try to reach a resource the tomcat server throws an error:
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.ja
We belive it started after he added jsoup dependency.
EDIT:
I edited my dependencies and web.xml, and now I only get 404 not found.
This is my dependencies form my pom.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
<dependency>
<groupId>org.facebook4j</groupId>
<artifactId>facebook4j-core</artifactId>
<version>[2.4,)</version>
</dependency>
</dependencies>
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>api</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Edit
If I try to reach: http://localhost:8080/api-mashup-api/api/v1/foobar
#Path("/v1")
public class API {
private Controller controller;
public API(){
controller = new Controller();
}
/**
* Prints to the screen if we are in /v1/foobar
*
* #return
*/
#GET
#Path("/foobar")
#Produces(MediaType.TEXT_HTML)
public String print2() {
return "Please specify what resource you need.";
}
I just get 404.
Probably your issue caused by wrong jersey-container-servlet which may cause the wrong uribuilder to be pickedup
https://jersey.java.net/documentation/latest/modules-and-dependencies.html#server-jdk
jersey-container-servlet => Jersey core Servlet 3.x implementation
jersey-container-servlet-core => Jersey core Servlet 2.x implementation
Change:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
to
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
Also refer https://jersey.java.net/documentation/latest/modules-and-dependencies.html#dependencies to find out which dependencies you need to provide for Glassfish and other Servlet based (non jax-rs integrated) containers
1) You have an AbstractMethodError exception which is thrown when an application tries to call an abstract method.
uri is an abstract method in UriBuilder, so you need an implementation of this.
You should use JAX-RS 2.0 with Jersey 2.* that implements JAX-RS 2.0 and contains an implementation to uri method.
2) By looking at your stack trace it clearly states that you are using both Jersey versions 1 & 2 and that's not possible, So the classloader is picking up the wrong URIBuilder class.
Stack Trace:
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
The Jersey dependencies in group com.sun.jersey are all Jersey version 1. Jersey version 2 uses the group org.glassfish.jersey.
I have read all topics related to my issue but solution still not found.
The problem is that when i use this dependency in pom.xml
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.1</version>
</dependency>
and this serlvet mapping in web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
everything works correctly.
But when i change dependency to:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.2</version>
</dependency>
and servlet mapping to:
<servlet>
<servlet-name>jerseyServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.myApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
then Tomcat give me 404 error.
The main cause for using second config entry is that i need async support, and as i understand this is possible only in Jersey 2.x
Can anyone help me to solve this issue?
You need the Jersey servlet dependency
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>
Also for async support, you should also add <async-supported>true</async-supported> to your Jersey servlet declaration
I am implementing rest easy web service and i am using jboss 4.0 but i am getting following exception...
java.lang.NoClassDefFoundError: javax/ws/rs/core/Configuration
my web.xml is
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>RestfulWebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- <servlet>
<servlet-name>HelloServlet</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet> -->
<!-- <servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>javax.ws.rs.core.Application</servlet-class>
</servlet> -->
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<!-- <context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param> -->
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.websevices.TestService</param-value>
</init-param>
</servlet>
<!-- <servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping> -->
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
It's easy to meet such error when migrating/copying dependencies from servlet based project to standalone. For servlet based project one usually needs dependency with scope = 'provided' relying that servlet provides implementation:
Dependency causing error:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
But for standalone projects there is no servlet implementation so you need to turn on copy javax.ws.rs-api to classpath. Usually you need just to remove <scope>provided</scope> line and have straightforward dependency:
Fixed dependency:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
From the Exception its clear that The class loader is not able to find the class Configuration (from javax/ws/rs/core/Configuration).
Please check classpath, corresponding JAR file and make sure that the class file exist.
This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
Morning, I already checked most of the answers to this problem (No mapping found for HTTP request with URI.... in DispatcherServlet with name) and also (No mapping found for HTTP request with URI [/ChickenTest/index] in DispatcherServlet with name 'dispatcherServlet') but I'm still getting "No mapping found for HTTP request with URI [/bmoa-surrounds/bmoa] in DispatcherServlet with name 'bmoa'", so, any help whould be apreciated:
pom:
<dependencies>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
then my web.xml
<display-name>bmoa-surrounds</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bmoa-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>bmoa</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bmoa</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
my spring config file
<context:component-scan base-package="xxxx"/>
<context:annotation-config/>
<context:spring-configured/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
and finally my controller
#Controller
public class BMOAServlet implements HttpRequestHandler {
/**
*
*/
#RequestMapping("/bmoa-surrounds/bmoa")
public void handleRequest(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
response.getWriter().write("result=" + handleIncomingMessage(request));
}
I'm calling "http:// localhost :8080/bmoa-surrounds/bmoa?juan=9898" but I'm still geting No mapping found for HTTP request with URI [/bmoa-surrounds/bmoa] in DispatcherServlet with name 'bmoa', any ideas? my env is java6 a deploying to jboss
also im sure that the beans are beign loaded, I got this in the server log
12:34:06,671 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-5) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#57ffa0: defining beans [BMOABussinesDelegate,properties,BMOAServlet,.........]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory#122d7c6
and also this
12:34:06,753 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-5) Mapped URL path [/bmoa-surrounds/bmoa] onto handler 'BMOAServlet' 12:34:06,754 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-5) Mapped URL path [/bmoa-surrounds/bmoa.*] onto handler 'BMOAServlet' 12:34:06,755 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-5) Mapped URL path [/bmoa-surrounds/bmoa/] onto handler 'BMOAServlet'
doesnt the last one means that the mappings are loaded?? please help ;(
I'm feeling really dumb right now....at first (and thanks for the clue Angad), the url-pattern was wrong, it should point to the servlet, also, the loaded bean was BMOAServlet instead of bmoa, so when I changed the url-patter no bmoa, managed to see the error, at the end my web.xml needed to look like this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bmoa-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>bmoa</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bmoa</servlet-name>
<url-pattern>/bmoa</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
and the bean class like this:
#Controller("bmoa")
public class BMOAServlet implements HttpRequestHandler {
/**
*
*/
#RequestMapping("/bmoa-surrounds/bmoa")
public void handleRequest(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
response.getWriter().write("result=" + handleIncomingMessage(request));
}
Now everything works smooth, I also changed the servlet class like this:
<servlet>
<servlet-name>bmoa</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Check this config file: src\main\webapp\WEB-INF\spring\appServlet\controllers.xml
content is like:
<context:component-scan base-package="org.springframework.samples.mvc" />
is your controller in "org.springframework.samples.mvc"?
In pom.xml make sure packaging is set to war like <packaging>war</packaging> ,not to jar or any thing else.
Here's a working web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<filter>
<filter-name>rememberMeCookieFilter</filter-name>
<filter-class>be.example.fun.jsp.filters.RememberMeCookieFilter</filter-class>
</filter>
<filter>
<filter-name>mustBeSignedInFilter</filter-name>
<filter-class>be.example.fun.jsp.filters.MustBeSignedInFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>rememberMeCookieFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>mustBeSignedInFilter</filter-name>
<url-pattern>/private/*</url-pattern>
</filter-mapping>
</web-app>
When I remove the <filter> elements and use the following annotations instead:
#WebFilter(filterName="rememberMeCookieFilter")
public class RememberMeCookieFilter implements Filter
#WebFilter(filterName="mustBeSignedInFilter")
public class MustBeSignedInFilter implements Filter
Then Tomcat 7.0.14 gives me the following error:
java.lang.IllegalArgumentException: Filter mapping must specify either a <url-pattern> or a <servlet-name>
at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:2956)
at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2915)
at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1180)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1270)
...
I followed the answer of this question, but that doesn't work for me.
Here are the dependencies of my web application:
<dependencies>
<!-- SLF4J (+ LOGBack) for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.8.3</version>
</dependency>
<!-- The servlet API that I installed in my local repo -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0</version>
<type>jar</type>
<scope>provided</scope>
<!--optional>false</optional-->
</dependency>
<!-- JUnit for testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
EDIT: I only have the issue when using Tomcat (7.0.14). Glassfish is fine.
It's a bug in Tomcat 7. I reported it as issue 53354.
As it's not possible to specify the invocation order in a #WebFilter, users are forced to explicitly specify <filter-mapping> in web.xml. This works in combination with a #WebFilter(filterName) in Glassfish and JBoss AS as follows:
#WebFilter(filterName="filter1")
public class Filter1 implements Filter {}
#WebFilter(filterName="filter2")
public class Filter2 implements Filter {}
with
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/url1/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/url2/*</url-pattern>
</filter-mapping>
However it fails in Tomcat 7.0.27 with the following confusing exception (the <url-pattern> is been set)
Caused by: java.lang.IllegalArgumentException: Filter mapping must specify either a <url-pattern> or a <servlet-name>
at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:3009)
at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2968)
at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1207)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
In the meanwhile your best bet is to use Glassfish or JBoss AS instead, or to register the filters by <filter> anyway.
You must specify a target for the Servlet Filter. Either provide a value for 'servletNames' or 'urlPatterns'.
http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebFilter.html
e.g.
#WebFilter(filterName="mustBeSignedInFilter", urlPatterns={ "/signed/in/path/*" })
public class MustBeSignedInFilter implements Filter