SpringMVC的@Requestbody接收不到json的值

控制层
@Controller
@RequestMapping("/task")
public class TaskAction {

@RequestMapping("/run")
public @ResponseBody TaskConnects runTask(@RequestBody TaskConnects connects)throws Exception{


    System.out.println("----------------------" + "\n" + connects);


    return connects;
}

}
js的代码
$.ajax({
type: "post",
url: "task/run.action",
contentType:'application/json;charset=utf-8',
data: '{"ConnectionId":"con_12","SourceId":"circle-1","TargetId":"roundedRect-3"}',
datatype: "json",
success: function () {
alert("send successfully")
}
SpringMVC配置文件也加了json转换器
<!-- json转换器 -->










text/html;charset=UTF-8
text/json;charset=UTF-8
application/json;charset=UTF-8


<mvc:annotation-driven />

SpringMVCxml文件完整配置
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

<context:component-scan base-package="cn.test.action" />
<!-- json转换器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJackson2HttpMessageConverter" />
        </list>
    </property>
</bean>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>text/html;charset=UTF-8</value>
            <value>text/json;charset=UTF-8</value>
            <value>application/json;charset=UTF-8</value>
        </list>
    </property>
</bean>

<mvc:annotation-driven />

<!-- 配置资源  ( For static resources ) -->    
<mvc:resources mapping="/html/**" location="/html/" />  
<mvc:resources mapping="/image/**" location="/images/" />    
<mvc:resources mapping="/js/**" location="/js/" />    
<mvc:resources mapping="/css/**" location="/css/" />    

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/html/" />
    <property name="suffix" value=".html" />
</bean>

var obj = {
"ConnectionId":"con_12",
"SourceId":"circle-1",
"TargetId":"roundedRect-3"

};
$.ajax({
type : "POST",
url : "task/run.action",
cache : false,
async : false,
contentType : "application/json; charset=utf-8",
data : JSON.stringify(obj),
........
你试试吧!

楼主你好,你写的这段JS:js的代码
$.ajax({
type: "post",
url: "task/run.action",
contentType:'application/json;charset=utf-8',
data: '{"ConnectionId":"con_12","SourceId":"circle-1","TargetId":"roundedRect-3"}',
datatype: "json",
success: function () {
alert("send successfully")
}
其中data内容在后台完全可以用普通的string类型去接收,或者是你自己创建一个searchDto接收

spring 中介入
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">


<!-- json转换器 -->



代码如下:
    <bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
    <list>
            <!-- json转换器 -->
            <ref bean="mappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>