Jquery 无法从带注释的控制器获取自动格式化的json数据

Jquery 无法从带注释的控制器获取自动格式化的json数据,jquery,json,spring-mvc,annotations,jackson,Jquery,Json,Spring Mvc,Annotations,Jackson,据我所知,如果您使用的是mvc:annotation-driven标记,那么您可以传回JSON格式的对象,前提是类路径中存在相关的jackson-jar文件。事实就是这样 但是,我从服务器得到了406(不可接受)响应 以下是注释控制器的相关部分: @RequestMapping(value = "/{groupEventUID}/{attendeeId}/update2.htm", method = RequestMethod.POST) public @ResponseBody DateRes

据我所知,如果您使用的是mvc:annotation-driven标记,那么您可以传回JSON格式的对象,前提是类路径中存在相关的jackson-jar文件。事实就是这样

但是,我从服务器得到了406(不可接受)响应

以下是注释控制器的相关部分:

@RequestMapping(value = "/{groupEventUID}/{attendeeId}/update2.htm", method = RequestMethod.POST)
public
@ResponseBody
DateResponse submitDate2(@PathVariable String groupEventUID, @PathVariable int attendeeId, ModelMap model) {
    return new DateResponse();
}
这是客户端页面上的jquery代码:

    <script type="text/JavaScript"> 
        $(document).ready(function(){
            $(".dayblock").click(function(){
                $(event.target).css('background-color','green');

                $.ajax({
                    url: "update2.htm",
                    type: "POST",
                    dataType: "json",
                    success: function(data){
                        alert("Data Loaded: " + data);
                    }
                }); 
            });
        });
    </script> 
下面是dispatcher-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >


    <context:component-scan base-package="com.crowdbot.nightout.web"/>
    <context:component-scan base-package="com.crowdbot.nightout.dao"/>


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

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>


    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />


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


    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
        </property>
    </bean>


    <bean id="myTxManager"
     class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
    </bean>

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


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

    <mvc:annotation-driven/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


</beans>
这是同期的本地主机日志:

07-Feb-2011 17:05:47 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
07-Feb-2011 17:05:47 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
07-Feb-2011 17:05:49 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
07-Feb-2011 17:05:49 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()

请确保在Spring配置xml中有对的引用

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

原因是:

<mvc:annotation-driven>

好的,它是固定的

问题在于我已经注册了DefaultAnnotationHadlerMapping和AnnotationMethodHandlerAdapter。我怀疑这些与标签设置的bean冲突。我想这就是当你尝试将一堆不同的教程混合在一起时会发生的事情

将它们注释掉可以解决问题

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


谢谢大家的帮助

看看如何用Firebug或Fiddler记录HTTP请求,并显示它。啊。。当我重新启动tomcat时,我得到以下消息:“匹配的通配符是严格的,但是找不到元素‘mvc:annotation driven’的声明”Eugh,这是一个XML错误。我使用注释的全部原因是为了避免使用XML,但它又把我拖回到了XML中。即使在使用注释时,您也必须始终在XML中配置某些内容。虽然注释会使您的xml配置文件变小。不,是我的错误,这是我之前在xml中输入错误时修复的一个旧错误@javi:我知道我永远无法摆脱XML的恐怖,但将其最小化是件好事:)我一直在这个网站上工作:它说:在封盖下面,Spring MVC委托HttpMessageConverter执行序列化。在本例中,SpringMVC调用构建在Jackson JSON处理器上的MappingJacksonHttpMessageConverter。当您使用mvc:annotation-driven配置元素并在类路径中显示Jackson时,将自动启用此实现。“那么这是错的吗?我必须显式启用它?好的,添加了显式引用。。。仍在使用406。你能发布你的日志吗?也许你有一些错误,只要看看我的代码,我就知道我在使用jackson-all-1.6.1.jar
<mvc:annotation-driven>
<!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->