Deploy Spring Boot application in Tomcat 6 (Traditional deployment) - java

I trying to deploy a spring boot application (war) in tomcat 6 ( when a deploy in tomcat 7 and older i get no problem ), in fact deploying in tomcat 6 (servlet 2.5) is not possible using the new way (i will put the link that describe the new way in the bottom) because Spring Boot uses Servet 3.0 APIs to initialize the ServletContext (register Servlets etc.) so you can’t use the same application out of the box in a Servlet 2.5 container.
the solution to deploy in spring that i found in the documentation (i will put the link in the bottom) is to add web.xml.
Now, when I send a request after deploying i get this error :
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'metricFilter' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1086)
at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:327)
at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:236)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:424)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
I use the same web.xml in the documentation :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>sofadev.docapost.phenix.server.changedot</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>metricFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>metricFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And of course the pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>sofadev</groupId>
<artifactId>docapost</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>docapost</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>sofadev.docapost.phenix.server.changedot.App</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.16</version>
</dependency>
<!-- <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version> </dependency> -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- marked the embedded servlet container as provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-legacy -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
</dependencies>
Link for the new way of deployement:
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm
Link for Traditional deployement (spring documentation):
https://docs.spring.io/autorepo/docs/spring-boot/1.2.0.RC1/reference/html/howto-traditional-deployment.html

You are going against the Spring Boot requirements by trying to deploy to Tomcat 6. Even if you hack your application to start something might break later. Since you are on Spring Boot 1.5.9.RELEASE the docs mention it clearly:
Tomcat 7 & 8.0 work with Spring Boot, but the default is to use Tomcat 8.5. If you cannot use Tomcat 8.5 (for example, because you are using Java 1.6) you will need to change your classpath to reference a different version.
Tomcat 6 is legacy and running it's a security risk. It's security support ended on 31 December 2016 and you can't download it since 30 March 2017. Upgrade your Tomcat version.

Related

Exposing Resources using Tomcat Server

After debugging for a few hours, I am seeking help to set up a REST API using Jersey in Java. I am using a Tomcat 6 server and the following is my file structure of the Mavin project:
On the right, the resource which I want to expose can be seen.
For the web.xml I have a file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details https://github.com/jax-rs -->
<web-app version="2.5">
<servlet>
<servlet-name>Jersey Web Application</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.howtodoinjava.demo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
and for the pom.xml as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava.demo</groupId>
<artifactId>JerseyArcheTypeDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JerseyDemo</name>
<build>
<finalName>JerseyArcheTypeDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.20</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I can build the Maven project and start the server. However if I want to add resources to the Server by right-clicking on the server name and clicking on "Add or Remove..." I get a message that I cannot select any resources. I followed the adivce in this question: There are No resources that can be added or removed from the server but it did not help and I also do not get any error messages which could lead me. Any recommendation what I am doing wrong?
When I want to run the Resource on the server by selecting "Run As" and then "Run on Server" I get the following message: The selection cannot be run on the server.
You can't select this project because Tomcat 6 does not support Servlet 4.0 projects. Servlet 4.0 requires Tomcat 9.x, which itself requires Java 1.8. See the Tomcat documentation.
You can lower the facet version, but it makes far more sense to simply use the newer Tomcat version. Tomcat 6 is far past its end-of-life.

ClassNotFoundException even after adding dependecy jars

I'm creating a simple REST web service using Jersey. And when I try to run it on the server(Tomcat 7) I'm getting the following exception. I can see in my Maven dependecy library that the class(com.sun.jersey.spi.container.servlet.ServletContainer) is already present.Need help.
Exception:
SEVERE: Servlet /SampleJersey threw load() exception
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
1.I have added the extra maven dependency core and servlet and tried. But it did not work.
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
I tried the following as well:
Right click on project --> Build Path --> Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0
Here's my pom.xml:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
And 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>
Here you are running the project as dynamic web project on a server within eclipse, so you should open the project properties and send the maven dependencies to the server directory by adding the maven dependency to deployment assembly.
Right click on Project -> properties -> deployment assembly -> add -> java build path entries -> maven dependency

Weblogic 12.2.1.3 from Moxy to Jackson

I would like to change the JSON parser from Moxy to Jackson, and I have followed the shared library approach, but without any result, Moxy is still used.
Note: the shared library is not necessary!!!
The shared library
I have created a maven project with the following dependencies and descriptors.
pom.xml
<properties>
<jackson.annotations.release>2.8.6</jackson.annotations.release>
<jersey.version>2.22.4</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson.annotations.release}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.annotations.release}</version>
</dependency>
<!-- Jersey dependencies -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependency>
weblogic.xml
<container-descriptor>
<prefer-application-packages>
<!-- apis -->
<package-name>javax.ws.rs.*</package-name>
<!-- guava -->
<package-name>com.google.common.*</package-name>
<!-- jersey providers -->
<package-name>org.glassfish.jersey.*</package-name>
<package-name>jersey.repackaged.*</package-name>
<!-- hk2 -->
<package-name>org.jvnet.hk2.*</package-name>
<package-name>org.jvnet.hk2.tiger_types.*</package-name>
<package-name>org.glassfish.hk2.*</package-name>
<package-name>javassist.*</package-name>
<!-- media providers -->
<!--<package-name>org.eclipse.persistence.jaxb.rs.*</package-name>-->
<package-name>com.fasterxml.jackson.*</package-name>
<!-- wls -->
<package-name>weblogic.jaxrs.api.client.*</package-name>
<package-name>weblogic.jaxrs.internal.api.client.*</package-name>
<package-name>weblogic.jaxrs.dispatch.*</package-name>
<package-name>weblogic.jaxrs.monitoring.util.*</package-name>
</prefer-application-packages>
</container-descriptor>
MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: Jackson Weblogic
Implementation-Version: 1.0
Specification-Title: Jackson Weblogic
Extension-Name: jackson-weblogic
Specification-Version: 1.0
Web Application
I add the web.xml and the weblogic.xml.
web.xml
<servlet>
<servlet-name>com.dummy.MyApplication</servlet-name>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>com.dummy.MyApplication</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
<library-ref>
<library-name>jstl</library-name>
<specification-version>1.2</specification-version>
<implementation-version>1.2</implementation-version>
<exact-match>false</exact-match>
</library-ref>
<library-ref>
<library-name>jackson-weblogic</library-name>
<specification-version>1.0</specification-version>
<exact-match>false</exact-match>
</library-ref>
</weblogic-web-app>
Then I reference in the weblogic.xml the shared library, but Moxy is still used. Have I done something wrong?
Update: I have added the web.xml and the weblogic.xml of the web application.
Solution
I have checked the folder oracle_common\modules and I have found all the Jackson dependencies needed. So I have preferred to configure Jersey to disable the use of MOXy as follows:
import javax.ws.rs.core.Application;
public class MyApplication extends Application
{
#Override
public Map<String, Object> getProperties()
{
Map<String, Object> proprties = new HashMap<>();
proprties.put("jersey.config.server.disableMoxyJson", true);
return proprties;
}
}
Pay attention:
that the method getProperties() comes with javax.ws.rs 2.0;
no specific shared library is necessary for Jackson, but just for jstl and jax-rs 2.0;
no info inside web.xml is required;
as reported by #abbas JAX-RS 2.0 comes by default with Weblogic 12.2.1.3.0 in my case I has a transitive dependency on javax.ws.rs:jsr311-api so the method getProperties() was not invoked.
Final weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
<context-root>/web-app</context-root>
<container-descriptor>
<prefer-application-packages>
<package-name>org.apache.commons.net.*</package-name>
</prefer-application-packages>
</container-descriptor>
<library-ref>
<library-name>jstl</library-name>
<specification-version>1.2</specification-version>
<implementation-version>1.2</implementation-version>
<exact-match>false</exact-match>
</library-ref>
</weblogic-web-app>
Mention JAX-RS 2.0 as a dependency in your pom.xml
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
In WebLogic 12.2.1 you do not need to deploy it as a shared library.

java.lang.NoClassDefFoundError: org/apache/poi/EncryptedDocumentException

I m building a web app and using apache poi for working on excel sheets. When I start the server I get the following exception. Thing is it used to work fine until i tried to build it and deploy in on heroku (but might be irrelevant).
Anybody has an idea??
INFO: Starting service Catalina
Apr 15, 2016 8:28:35 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.53
Apr 15, 2016 8:28:36 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.engine.OnStartup
java.lang.NoClassDefFoundError: org/apache/poi/EncryptedDocumentException
at org.engine.OnStartup.contextInitialized(OnStartup.java:16)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.EncryptedDocumentException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
... 10 more
My pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>psilos-toy</groupId>
<artifactId>psilos-toy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.httpcache4j.resolvers</groupId>
<artifactId>resolvers-net-urlconnection</artifactId>
<version>4.0-M6</version>
</dependency>
<!-- Apache POI,for handling Excel Files -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
and my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.endpoints</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.engine.OnStartup</listener-class>
</listener>
</web-app>
Perhaps it was working on a Tomcat setup that had poi classes available on the classpath. In the new Heroku setup, perhaps you don't have those classes available with Tomcat 7 (I haven't thoroughly checked if poi classes come bundled with Tomcat 7).
The easiest thing you can do is to build a self-contained app by bundling the poi classes along with your app. This increase your app's size, but you get to control the poi library version. Of course, you can always copy the libraries to a defined folders (where you know Tomcat loads the classes from). See maven-shade-plugin for details.

Using Tomcat, #WebFilter doesn't work with <filter-mapping> inside web.xml

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

Categories

Resources