Skip to content

Annotated Groovy controllers don't work properly [SPR-6268] #10935

Closed
@spring-projects-issues

Description

@spring-projects-issues

Szczepan Kuzniarz opened SPR-6268 and commented

In the following example:

package test
@Controller
class TestController {
    @RequestMapping(value = "/testGroovy.do", method = RequestMethod.GET)
    public String handler(HttpServletRequest request) {
        return "success"
    }
}
<context:annotation-config/>
<context:component-scan base-package="test"/>
<lang:groovy script-source="classpath:test/TestController.groovy" refresh-check-delay="1000"/>
<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

DefaultAnnotationHandlerMapping class discovers the Groovy controller and registers a handler as it logs:

INFO: Mapped URL path [/testGroovy.do] onto handler [test.TestController@134237]

but accessing such a controller causes ServletException:

javax.servlet.ServletException: No adapter for handler [test.TestController@134237]: Does your handler implement a supported interface like Controller?
    org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:780)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I'm not 100% sure, but it seems AnnotationMethodHandlerAdapter.getMethodResolver is unable to determine proper type of the handler. It uses ClassUtils.getUserClass (which returns proxy as a type) but for Groovy script the right type is it's MetaClass (getMethodResolver should use something like ((groovy.lang.GroovyObject) handler).getMetaClass().getTheClass()).

Maybe the problem with Groovy scripts is more general, and not limited to a MVC controller case?


Affects: 3.0 RC1

Issue Links:

Referenced from: commits 64fd0b0, 8e35734

1 votes, 4 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions