Spring Mvc Http 400 Bad Request问题排查

如果遇到了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 &#64;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注解修饰的,所以生成了单参构造函数。

参考

猜你喜欢

转载自www.cnblogs.com/gcdd/p/12292319.html