GlassFish / Jersey Exception "java.lang.IllegalArgumentException: object is not an instance of declaring class" And Solution

The exception:


SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.IllegalArgumentException: object is not an instance of declaring class
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
	at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
	at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
	at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
	at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
	[…]


is caused by exposing EJB 3.1 methods directly via REST without having a no-interface view declared. This happens when your EJB 3.1 REST-endpoint implements an additional interface without declaring the no-interface view:

import javax.ejb.*;

@Path("RESTafari)
@Singleton
public class RESTEndpoint implements SomeLocalInterface{}


Adding a @LocalBean annotation (and activating the no-interface view) solves the problem:


import javax.ejb.*;

@LocalBean
@Path("RESTafari)
@Singleton
public class RESTEndpoint implements SomeLocalInterface{}

Comments:

This is indeed very discouraging. I faced the same problem when working with local EJB - the simple stateless bean implementing some non-ejb interface must be annotated with @LocalBean to provide the possibility to inject bean through CDI. The exception was non-intuitive and it took long time for me to get the reason. As I saw in weld forum Gavin King was too sorry about this as he was against such additional actions and wanted to avoid additional annotations in this case.

Posted by Anton on February 01, 2012 at 09:27 AM CET #

Thank you! You make my day!

Posted by Hendrik on May 02, 2012 at 02:34 PM CEST #

Thanks

Should I annotate my facade Ejb which is a REST interface to my other Local Ejb with @LocalBean ?

Posted by Ehsun on April 19, 2013 at 07:19 PM CEST #

Thanks a lot!

Posted by 82.73.233.107 on April 17, 2016 at 09:58 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License