sinat_26778851 2016-10-14 09:24 采纳率: 0%
浏览 5998

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 />
  • 写回答

5条回答 默认 最新

  • sinat_26778851 2016-10-14 09:29
    关注

    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>
    

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog