我的控制器中有以下代码
@RequestMapping(value = "employee/update", method = RequestMethod.POST, headers = "Accept=application/json")
public UpdateEmployeeResponse updateEmployee(@RequestBody @Valid @ModelAttribute("updateEmployeeRequest") UpdateEmployeeRequest updateEmployeeRequest, BindingResult result) {
我的Request对象如下
public class UpdateEmployeeRequest {
@Valid
@NotNull
private Employee employee;
.
.
public class Employee {
@NotNull
protected String id;
@NotNull
protected String name;
.
.
当我发送JSON请求时(如果缺少id)
{employee:{name:"cc",phone:"9876543210",dept:"dpt"}}
我的请求未通过spring验证(即使缺少字段也不显示任何错误)。我已经完成了以下主题,但没有运气。
有人可以帮忙吗?
答案 0 :(得分:1)
要毫无例外地忽略JSON输入中的任何未知属性,请尝试使用@JsonIgnoreProperties(ignoreUnknown=true)
。
试试这个
<强> Employee.java 强>
@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee
{
@NotNull
protected String id;
@NotNull
protected String name;
.
.
<强> UpdateEmployeeRequest.java 强>
@JsonIgnoreProperties(ignoreUnknown = true)
public class UpdateEmployeeRequest {
@Valid
@NotNull
private Employee employee;
.
.
答案 1 :(得分:0)
您没有提及项目中是否有验证器。在Maven中添加这些依赖项:
<!-- Bean validation -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>