zoukankan      html  css  js  c++  java
  • 解决 spring mvc3.1下post json出现HTTP Status 400 The request sent by the client was syntactically incorrect

    问题描述:

    已声明

    @RequestMapping(value="update", method = RequestMethod.POST) 
        @ResponseBody 
        public Map<String, Result> updateNavi(@RequestBody Navigation model)

    启动日志有:

    Mapped "{[/navi/update],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, com.apusic.ebiz.framework.web.controller.Result> com.mm.NavigationController.updateNavi(com.mm.navigation.Navigation)

    执行

    $.postJSON('http://localhost:8082/mm/navi/update',{a:1})

    报400错误.

    $.postJSON('http://localhost:8082/mm/navi/update',{})

    没有错.

    他大爷的,之前一直使用这个方法,提交表单完全正常。见鬼了。


    排错

    1.控制台无任何信息,认为请求根本没有进系统,以被tomcat档住了,怀疑是web.xml,spring-mvc.xml等配置问题

    但修改web.xml,spring-mvc的N多参数无果。但想想,之前都可以成功,为啥突然不成功呢,奇怪了。

    2.怀疑构造的post的data不是json格式的,专门从一个能成功提交的json格式对比一下,完全一样。这下傻眼了、

    3.为什么空JSON构造能成功呢?请求直接到了Controller,不知道

    4.当我尝试使用

    $.postJSON('http://localhost:8082/mm/navi/update',””)

    它居然报错了,原来,他进了系统,返回错误给TOMCAT,所以tomcat才报错的400

    果断在org.springframework.web.servlet.DispatcherServlet.doDispatch(HttpServletRequest, HttpServletResponse)打断点

    果然,进来了,一步步跟,发现下面有log.debug,原来有日志的,而我的控制台没日志,郁闷,一看log4j,果然是info级别的


    解决:

    打开log4j对org.springframework.web=debug

    再次请求

    $.postJSON('http://localhost:8082/mm/navi/update',{a:1})

    终于见到异常了

    Resolving exception from handler org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "a" (Class com.mm.navigation.Navigation), not marked as ignorable 
    at [Source: org.apache.catalina.connector.CoyoteInputStream@8f74ba; line: 1, column: 7] (through reference chain: com.apusic.mm.navigation.Navigation["a"]); nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "a" (Class com.mm.navigation.Navigation), not marked as ignorable 
    at [Source: org.apache.catalina.connector.CoyoteInputStream@8f74ba; line: 1, column: 7] (through reference chain: com.apusic.mm.navigation.Navigation["a"])

    之前异常都被 spring吃了,烦啊。

    原来是实体Navigation里没有a的属性,转换不成功,抛出异常,却被spring转成400错误,简单丢出来了,spring厚道啊。

    再次请求有的参数,

    $.postJSON('http://localhost:8082/mm/navi/update',{id:1})

    果然好了。

    仅写此,共享之,启发一下,问题虽然简单,但有时候脑子容易短路,找不到点。

    附:

    $.postJSON = function(url, data, callback) { 
        return jQuery.ajax({ 
            'type': 'POST', 
            'url': url, 
            'contentType': 'application/json', 
            'data': JSON.stringify(data), 
            'dataType': 'json', 
            'success': callback 
        }); 
    };

  • 相关阅读:
    ArcObject获取ArcMap默认地理数据库的路径
    标准IO
    进程关系
    进程环境
    C语言基础知识位运算
    Bash 快捷键
    信号
    UNIX系统文件
    进程
    unix 文件属性
  • 原文地址:https://www.cnblogs.com/stevenx1987/p/4208688.html
Copyright © 2011-2022 走看看