开发者

How to enable Spring 3.0 MappingJacksonHttpMessageConverter with @ResponseBody AND pre-Spring-3.0 Controller SimpleUrlHandlerMapping together?

开发者 https://www.devze.com 2023-03-08 08:57 出处:网络
As the title suggests, I\'m trying and failing to get the following combination working in Spring 3.0:

As the title suggests, I'm trying and failing to get the following combination working in Spring 3.0:

  • pre-Spring-3.0 controllers mapped with SimpleUrlHandlerMapping, and,
  • a Spring-3.0 @Controller using MappingJacksonHttpMessageConverter and @ResponseBody to return JSON.

All the pieces work - except when put together!

In more detail, I have an existing Spring web-app which includes many pre-Spring-3.0 controllers. These implement Controller and are mapped explicitly with a SimpleUrlHandlerMapping bean. (So one solution is to change them all to @Controller style). The same web-app (DispatcherServlet) also supports several newer controllers annotated with @Controller.

My bean config includes the following, and all is good:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/path/name.ext">mySpring25Controller</prop>
     开发者_如何学Python   </props>
    </property>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>

Now I'm adding, to a new controller annotated with @Controller and @RequestMapping, use of MappingJacksonHttpMessageConverter so that with @ResponseBody some of my methods can return JSON via Jackson - to Ajax calls. Again all is good:

<!-- to generate JSON responses using Jackson (without using <mvc:annotation-driven/>) -->  
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <list>
        <ref bean="jacksonMessageConverter"/>
      </list>
    </property>
</bean>     

The problem is that now my explicit mappings defined in the SimpleUrlHandlerMapping bean are no longer working:

[http-8081-Processor25] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mayapp].[springapp]  - Servlet.service() for servlet springapp threw exception
javax.servlet.ServletException: No adapter for handler [com.mycom.controller.mySpring25Controller ...]: Does your handler implement a supported interface like Controller?
    at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:985)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:773)

These mappings still appear in my log at start-up (SimpleUrlHandlerMapping - Mapped URL path ... etc) - but evidently are now broken somehow.

INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping  - Mapped URL path [/path/name.ext] onto handler [com.mycom.controller.mySpring25Controller ....]

I started with <mvc:annotation-driven/> which had the same problem. I've tried re-ordering without success, including order properties. And I've also not found an explanation in the Spring docs. It seems there is some interaction in the Spring auto-magic which I haven't got to the bottom of.

Interesting problem anyone? Insight gratefully received!


Post and ye shall find (delayed by 8 hours for lack of reputation)!

As per post No adapter for handler exception the answer appears to be that the explicit AnnotationMethodHandlerAdapter definition blows away the implicit SimpleControllerHandlerAdapter which was previously satisfying the SimpleUrlHandlerMapping mappings.

So add it explicitly:

<bean
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>

And we're good, though at the same time you might also need to introduce also an explicit replacement for the implicit HttpRequestHandlerAdapter also blown away:

<bean
    class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter">
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号