Java Spring启动-验证请求主体json密钥

Java Spring启动-验证请求主体json密钥,java,json,spring-boot,Java,Json,Spring Boot,我正在SpringBoot中验证请求主体。 当post控制器使用下面的JSON在DB中创建记录时。它很好用 { "test1": "string", "test2": "string", "test3": "string", <--this has @Null in the entity "test4": "string" } 您可以使用解析JSON和处理未知属性。如果发现未知属性,它将自动抛出UnrecognizedPropertyException,如所述是@Nul

我正在SpringBoot中验证请求主体。 当post控制器使用下面的JSON在DB中创建记录时。它很好用

{
  "test1": "string",
  "test2": "string",
  "test3": "string",  <--this has @Null in the entity
  "test4": "string"
}

您可以使用解析JSON和处理未知属性。如果发现未知属性,它将自动抛出UnrecognizedPropertyException,如所述

@Null
还是
@NotNull
。是否可以提供一段最小的可复制代码,以便我们更好地了解上下文并帮助它@Null,因此如果该字段拼写错误,它将显示“test3”=Null,因为它可以为Null值。添加了示例实体类test1为id,test2为NotNull,test3和test4允许Null值。@NotNull(message=”“)您可以给messageIt假定允许test3和test4使用null值。当请求JSON没有“test3”时,它将替换空值。如果用户在字段“test3”->“test5”中出错,则不会显示错误。
{
  "test1": "string",
  "test2": "string",
  "test5": "string", <- wrong key by mistake
  "test4": "string"
}
@Entity
@Table(name = "test")
@Data
@NoArgsConstructor
public class Test implements Serializable {

@Id
@Column(name = "test1")
private String test1;

@Column(name = "test2")
@NotNull
private String test2;

@Column(name = "test3")
private String test3;

@Column(name = "test4")
private String test4;
}
If u want to Validate request body in JSON u can use @Valid

      @PostMapping("/books")
    Book newBook(@Valid @RequestBody Test test) {
        return repository.save(Test);
    }

@Column(name = "test3")
@NotNull(message = "Please provide a test3")
private String test3;

if u want on key order
 JsonPropertyOrder({ "test1", "test2", "test3", "test4" })
public class Test implements Serializable {
}