ClassCastException for GuiceFilter with Guice 2.0 and JBoss 4.2.2

551 views
Skip to first unread message

bysse

unread,
Mar 11, 2010, 5:20:41 AM3/11/10
to google-guice
I'm getting a ClassCaseException when i try to use GuiceFilter on
JBoss 4.2.2.
It's probably because of different versions of servlet-api (2.4 in
JBoss) but i can't find a way around it. Have anyone had this problem
and solved it? J

java.lang.ClassCastException: com.google.inject.servlet.GuiceFilter
cannot be cast to javax.servlet.Filter
at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:
255)
at
org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:
397)
...

Boss 5 is unfortunately not an option here. My configuration works
flawless in my tests with Jetty...

thanks
Erik

Brandon Atkinson

unread,
Mar 11, 2010, 3:10:19 PM3/11/10
to google...@googlegroups.com
I have had this problem before.

Are you using maven by any chance? 

If so, make sure that you've made servlet-api a provided dependency.

For some reason, guice-servlet's pom declares that it has a compile time dependency on servlet-api.  This causes the servlet-api classes to be loaded into your war.  Since there are two defined copies of the servlet api classes (in two different class loaders) a CCE occurs.

-Brandon


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Dhanji R. Prasanna

unread,
Mar 11, 2010, 7:16:24 PM3/11/10
to google...@googlegroups.com
On Fri, Mar 12, 2010 at 7:10 AM, Brandon Atkinson <brandon.n...@gmail.com> wrote:
I have had this problem before.

Are you using maven by any chance? 

If so, make sure that you've made servlet-api a provided dependency.

For some reason, guice-servlet's pom declares that it has a compile time dependency on servlet-api.  This causes the servlet-api classes to be loaded into your war.  Since there are two defined copies of the servlet api classes (in two different class loaders) a CCE occurs.

messy =(

what do you suggest we do to our pom to fix this?

Dhanji.

Brandon Atkinson

unread,
Mar 11, 2010, 9:44:25 PM3/11/10
to google...@googlegroups.com
Guice servlet's 'servlet-api' dependency should be declared in 'provided' scope.  The sematics of this are described here:
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
In short, a provided dependency is fetched and put in the compile and test classpaths, but is not packaged, because it is expected to be provided at runtime by the container/jvm/etc..

...
<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <scope>runtime</scope>
</dependency>
...

In this case, the inaccuracy in the pom is not such a big deal.  An easy way around this for guice-servlet users is to declare the servlet-api in their pom as a provided dependency.  Maven will then ignore the servlet-api dependency during packaging.  A purist would say declaring a transitive dependency as a direct dependency in a parent project to make up for inaccurate dependency declarations in a library isn't right because the dependent project may not really use any classes in the dependency.  However, when using guice-servlet, the chances you don't depend on the servlet api are slim, since the whole point is that you using it for writing guicy servlet based apps.

-Brandon

Gary Pampara

unread,
Mar 12, 2010, 12:55:16 AM3/12/10
to google...@googlegroups.com
Either way, the servlet-api should be available within the web
container that is being used. Placing it in 'provided' scope would be
the correct solution as the container should be the source of the jar.

On Fri, Mar 12, 2010 at 4:44 AM, Brandon Atkinson

Stuart McCulloch

unread,
Mar 12, 2010, 1:47:54 AM3/12/10
to google...@googlegroups.com
On 12 March 2010 10:44, Brandon Atkinson <brandon.n...@gmail.com> wrote:
Guice servlet's 'servlet-api' dependency should be declared in 'provided' scope.  The sematics of this are described here:
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
In short, a provided dependency is fetched and put in the compile and test classpaths, but is not packaged, because it is expected to be provided at runtime by the container/jvm/etc..

...
<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <scope>runtime</scope>

^ that should read <scope>provided</scope> I believe (to avoid further confusion)
 

Brandon Atkinson

unread,
Mar 12, 2010, 7:07:38 PM3/12/10
to google...@googlegroups.com
yes... provided scope, not runtime.

That was a typo.
Reply all
Reply to author
Forward
0 new messages