MessageConverter未被调用

时间:2012-09-25 15:19:12

标签: json spring spring-mvc jackson

我在Spring中注册了一个自定义MessageConverter,配置如下:

<bean id="jsonHttpMessageConverter" class="com.eventwiz.web.util.ServiceResponseHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json"/>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <util:list id="beanList">
        <ref bean="jsonHttpMessageConverter"/>
      </util:list>
    </property>
</bean>

然而,它没有被调用,因为我在代码中使用断点确认了。 ServiceResponseHttpMessageConverter子类MappingJacksonHttpMessageConverter并覆盖writeInternal()方法。我甚至尝试重写MessageConverter.supports()只是为了看看是否正在调用它而不是。有什么想法正在发生什么?

1 个答案:

答案 0 :(得分:1)

根据作者的回答发现的问题是<mvc:annotation-driven/>标记,它注册了自己的handlerAdapter,因此如果将另一个handlerAdapter添加到Spring MVC配置文件中,并将转换器添加到此适配器,自定义适配器不会生效。修复方法是通过<mvc:message-converters...下的<mvc:annotation-driven标记注册httpMessageConverters或删除<mvc:annotation-driven并使用自定义handleradapter并在其下注册httpmessageconverter。

相关问题