2

提前感谢任何帮助。

我试图让我的控制器方法之一返回 JSON。从一个简单的测试开始:

@RequestMapping(value="/myReqPath", method=RequestMethod.GET)
@ResponseBody
public Map<String, String> myJsonMethod() {

    Map<String, String> response = new TreeMap<String, String>();
    response.put("test", "test");

    return response;

}

据我了解,我需要<mvc:annotation-driven/>将其添加到我的 servlet 上下文中才能完成此操作。问题是当我添加它时,它会破坏我的自定义 AnnotationMethodHandlerAdapter。

[B]如何提取和添加<mvc:annotation-driven/>从控制器返回 JSON 所需的部分?[/B]

以下是我的 servlet 配置的相关部分:

<!--Skipping this for now...
<mvc:annotation-driven/>
-->

<!-- JSON Marshaling -->
<util:constant id="jsonBasicClassIntrospector"
    static-field="org.codehaus.jackson.map.introspect.BasicClassIntrospector.instance" />
<bean id="jsonJaxbAnnotationIntrospector" 
    class="org.codehaus.jackson.xc.JaxbAnnotationIntrospector" />
<bean id="jsonVisibilityChecker" 
    class="org.codehaus.jackson.map.introspect.VisibilityChecker.Std"
    factory-method="defaultInstance" />
<bean id="jsonDefaultTypeFactory" 
    class="org.codehaus.jackson.map.type.TypeFactory"
    factory-method="defaultInstance" />
<bean id="jsonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
    <property name="serializationConfig">
        <bean class="org.codehaus.jackson.map.SerializationConfig">
            <constructor-arg ref="jsonBasicClassIntrospector" />
            <constructor-arg ref="jsonJaxbAnnotationIntrospector" />
            <constructor-arg ref="jsonVisibilityChecker" />
            <constructor-arg><null/></constructor-arg>
            <constructor-arg><null/></constructor-arg>
            <constructor-arg ref="jsonDefaultTypeFactory" />
            <constructor-arg><null/></constructor-arg>
        </bean>
    </property>
    <property name="deserializationConfig">
        <bean class="org.codehaus.jackson.map.DeserializationConfig">
            <constructor-arg ref="jsonBasicClassIntrospector" />
            <constructor-arg ref="jsonJaxbAnnotationIntrospector" />
            <constructor-arg ref="jsonVisibilityChecker" />
            <constructor-arg><null/></constructor-arg>
            <constructor-arg><null/></constructor-arg>
            <constructor-arg ref="jsonDefaultTypeFactory" />
            <constructor-arg><null/></constructor-arg>
        </bean>
    </property>
</bean>

...

<!-- My custom  AnnotationMethodHandlerAdapter... -->
<bean id="sessionArgResolver" class="com.SessionParamArgumentResolver"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="customArgumentResolver" ref="sessionArgResolver"/>
</bean>

就目前而言,我的控制器方法被调用,但是浏览器返回 http 状态 406: 406 Not Acceptable - [url]http://localhost:8080/myApp/myReqPath[/url]

4

1 回答 1

3

如果您已经有自定义AnnotationMethodHandlerAdapter声明,则只需向其中添加HttpMessageConverters 列表:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="customArgumentResolver" ref="sessionArgResolver"/>
    <property name = "messageConverters">
        <list>
            <bean 
                class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name = "objectMapper" ref = "jsonObjectMapper" />
            </bean>
        </list>
    </property>
</bean> 
于 2012-04-19T13:48:44.580 回答