Spring 4 Webservice HTTP 500 - IllegalStateException: The mapped controller method class is not an instance of the actual controller bean

StackOverflow https://stackoverflow.com//questions/24058371

Question

so I am working on a school project and I am trying to build a JSON Rest Webservice application. I am using Spring 4 and Hibernate 4 with Jackson 2.

I have a lot of hard times with this app, but now I have a problem I cannot overgo. I am using Cloudbees as my cloud service provider and from time to time (which is important to state, because it sometimes work and sometimes not!) I get a HTTP 500 error :/. The best part is - I never had it locally.

It goes more or less like this:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: The mapped controller method class 'pl.lodz.pp.controllers.crud.impl.UserController' is not an instance of the actual controller bean instance 'com.sun.proxy.$Proxy47'. If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.

And I am so confused. I never get this locally and usually if I restart the application on cloud (one or more time) it will work for some time again.

I had made some errors, like

@Autowire
private ClassType variable 

instead of

@Autowire
private ClassInterface variable 

but I fixed them all. I am NOT USING @Transactional annotation anymore. At least not in my class. Maybe the GenericDao have it somewhere (https://code.google.com/p/hibernate-generic-dao/) but I never had this problem before.


Transaction management:

<bean id="transactionManager"
   class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

Please, find the full code here: https://github.com/atais/PP-JSON


Bottom line I am not using @Transactional and I inject everything with the Interface type. So what am I possibly doing wrong? And what's the best - it works sometimes, but sometimes I get this error :/

Was it helpful?

Solution

In many cases the framework applies for your classes some proxying mechanisms: TX, Cache, Async etc. - depends on annotations, which you are using on your classes or its methods.

So it is good practice to introduce for those classes interfaces and use exactly that contract, not classes.

I understand that it looks like overhead for all @Service classes, but introduce interfaces at least for those classes which use some AOP aspects

OTHER TIPS

The only thing you need to do is to add

@EnableAspectJAutoProxy(proxyTargetClass = true)

to your Spring Configuration.

I got the same exception when i implemented a interface (controller extends x implements interface) in my controller. Removing the interface solved the problem. I suspect implementing a specific interface is not supported for controllers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top