如果遇到了Spring MVC报错400,而且没有返回任何信息的情况下该如何排查问题?

问题描述

一直都没毛病的接口,今天测试的时候突然报错400 Bad Request,而且Response没有返回任何信息。

解决方案

尝试了一下午,终于找到了排查这类问题的办法。

我们知道,在Spring MVC里面,

org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

负责所有异常的统一处理。我们只要在方法handleException打上断点即可。

点开发现,原来是Lombok的问题。报错如下

Could not read JSON document: Can not construct instance of xxx: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: java.io.PushbackInputStream@12544acd; line: 2, column: 5]

Lombok没有为我们自动生成类的构造函数。我们在目标类加上@NoArgsConstructor即可解决。

刨根问底

为什么Lombok自动生成的类,没有可供Jackson反序列化的构造函数呢?我看了一下生成的字节码文件,里面确实不存在无参构造和全参构造函数,唯一的构造函数是带一个参数的。

目标类使用了@Data注解,而@Data注解的声明如下

/**
* Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
* all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
* <p>
* Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/Data">the project lombok features page for @Data</a>.
*
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
String staticConstructor() default "";
}

简单来说,@Data包含了以下注解的功能

  • @Getter
  • @Setter
  • @RequiredArgsConstructor
  • @ToString
  • @EqualsAndHashCode

而“罪魁祸首”就是@RequiredArgsConstructor了,它的作用是

为每个需要特殊处理的字段(final修饰的或者是@NotNull注释的字段)生成一个带有1个参数的构造函数。

而目标类恰巧有一个字段就是@NotNull注解修饰的,所以生成了单参构造函数。

参考

Spring Mvc Http 400 Bad Request问题排查的更多相关文章

  1. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  2. spring mvc ajax 400解决

    The request sent by the client was syntactically incorrect. ajax发起请求时报400错误.请求代码如下: var reportId=($( ...

  3. spring mvc文件上传,request对象转换异常

    spring 文件上传有现成的工具用起来也挺简单.就是在还不是非常熟悉的时候可能会出一些错. 近期碰到了 org.apache.catalina.connector.RequestFacade can ...

  4. Spring MVC 中RequestContextHolder获取request和response

    1.最简单方式:处理方法入参 例如: @RequestMapping("/test") @ResponseBody public void saveTest(HttpServlet ...

  5. Spring MVC报错:The request sent by the client was syntactically incorrect ()

    原因: 1.参数名称不一致 2.参数类型不一致

  6. A real ROCA using Bootstrap, jQuery, Thymeleaf, Spring HATEOAS and Spring MVC

    http://www.tuicool.com/articles/ENfe2u https://github.com/tobiasflohre/movie-database What is the be ...

  7. SpringBoot系列: Spring MVC视图方法的补充

    SpringMVC 视图方法的参数, 已经在这个文章中写得非常清楚了, 链接为 https://www.cnblogs.com/morethink/p/8028664.html 这篇文章做一些补充. ...

  8. 基于Maven的Spring + Spring MVC + Mybatis的环境搭建

    基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...

  9. Spring MVC -- 下载文件

    像图片或者HTML文件这样的静态资源,在浏览器中打开正确的URL即可下载,只要该资源是放在应用程序的目录下,或者放在应用程序目录的子目录下,而不是放在WEB-INF下,tomcat服务器就会将该资源发 ...

随机推荐

  1. 图像GIST特征和LMGIST包的python实现(有github)

    1什么是Gist特征        (1) 一种宏观意义的场景特征描述        (2) 只识别"大街上有一些行人"这个场景,无需知道图像中在那些位置有多少人,或者有其他什么对 ...

  2. 024.Python模块OS模块

    一 OS模块 对系统进行操作 1.1 popen 可以把运行的结果,这个字符串转化成utf-8这样的编码格式在进行输出 import os res = os.popen("ifconfig& ...

  3. pyhton3之进程

    一 什么是进程 首先要了解什么是进程,程序是写出来没有被执行的代码,它是没有生命的实体,只有处理器赋予程序生命时,及程序被操作系统运行起来,他是一个活动的实体,我们称其为进程. 进程是计算机中的程序关 ...

  4. Spring注入bean的方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式).   使用属性的se ...

  5. python学习记录(七)

    0904--https://www.cnblogs.com/fnng/archive/2013/04/24/3039335.html 0904--https://www.cnblogs.com/fnn ...

  6. Asp.Net Core IdentityServer4 管理面板集成

    前言 IdentityServer4(以下简称 Id4) 是 Asp.Net Core 中一个非常流行的 OpenId Connect 和 OAuth 2.0 框架,可以轻松集成到 Asp.Net C ...

  7. ORACLE-SQLLOAD导入外部数据详解

    今天公司需要把外部文本的一些数据导入到数据库.这里把相关步骤和注意的地方记录,供需要的人参考学习!这里的环境是在windows下的数据库,linux或者其他数据库同理! 1.准备工作:创建需要导入数据 ...

  8. 实验楼-python3简明教程笔记

    #!/usr/bin/env python3 days = int(input("Enter days: ")) print("Months = {} Days = {} ...

  9. Python - os.walk()详细使用

    os.walk() 方法简单介绍 主要用来遍历一个目录内各个子目录和子文件 是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 方法参数介绍 os.walk(top[, t ...

  10. bat常用符合和for语句等

    一.开头 @echo off(默认是echo on)@echo off执行以后,后面所有的命令均不显示,包括本条命令 二.特殊符号 1. | 命令管道符,echo Y|rd /s c:\abc,通过管 ...