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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题