no String-argument constructor/factory method to deserialize from String value

写了个web项目,总是报错,如下:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.cetc.modules.sys.entity.SysUserEntity` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (','); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.cetc.modules.sys.entity.SysUserEntity` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (',')
 at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241)

其实是前端获取的json格式有问题,需要将form表单转换为Json数据

var formdata = $("#form").serializeJSON();

但是必须要有<script type="text/javascript" src="../../js/jquery.serializejson.js"></script>,否则报错找不到serializeJSON()方法

jquery.serializejson.js通过https://www.bootcdn.cn/jquery.serializeJSON/可以获取。

然后通过ajax:

$.ajax({
                type : 'post',
                url : '/badInfo',
                contentType: "application/json; charset=utf-8",  
                data : JSON.stringify(formdata),
                success : function(data) {
                    $("#resInfo").val(data);
                }

其中JSON.stringify(formdata), 从一个对象中解析出字符串,就可以传到Controller了。

猜你喜欢

转载自blog.csdn.net/keep12moving/article/details/84590442