@RequestBody不能映射到对象

1,在配置SpringMVC的时候在配置文件中配置了

<mvc:annotation-driven>但是在前后端交互的时候传输JSON在controller中接受参数的时候不能映射到对象中


<script type="text/javascript">
    $(document).ready(function(){
        $('#submitForm').click(function(){

            var formData = new Object;
            formData.Id = 2;
            formData.UserName = $('#UserName').val();

            var JsonData = JSON.stringify(formData);
            console.log(JsonData);
            $.ajax({
                type : "POST",
                url : "/core/test",
                dataType : 'text',
                contentType: "application/json",
                data : JsonData,
                success : function(data) {
                    alert(data);
                }
            });
        });
    });
script>


package com.yk.controllers;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.yk.dto.userDto;
import com.yk.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class userController {

    @Autowired
    private  IUserService service1;

    @RequestMapping(value = "/test",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    //@JsonIgnoreProperties(ignoreUnknown = true)//忽略未知属性
    public Object query(@RequestBody userDto dt) {

        System.out.println(dt.getUserName());
        List<userDto> dto = service1.selectAll();

        System.out.println(dto.get(0).getUserName());

        return  "1";
    }
}



找了很多方法都没解决掉后来在配置中添加了
FastJsonHttpMessageConverter后台能接收到参数并且映射到对象中

<context:component-scan base-package="com.yk"/>
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            
        bean>
    mvc:message-converters>
mvc:annotation-driven>






你可能感兴趣的:(SpringMVC,spring,mvc)