REST请求有效负载的验证

时间:2016-08-11 13:04:33

标签: java web-services rest validation spring-mvc

我正在尝试使用" hibernate-validator-5.1.3.final"对REST请求应用验证。 jar。我能够为外豆而不是内豆做。所以任何人都可以帮我做。谢谢。

通过邮递员提出

{
    "employeeDetails": [{
                           "employeeId": "123456",
                           "dept" : 101,
                           "salary" : 30000,
                           "status":"active"                              
                }]
}

的pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.3.Final</version>
</dependency>

控制器

@RequestMapping(method = RequestMethod.POST, value = "/addEmployee", headers = "Accept=application/json")
    public @ResponseBody void addEmployee(
            @RequestHeader @Valid @RequestBody AddEmployeeBean requestBean,
            HttpServletRequest httpRequest) {.....}

Outer Bean - AddEmployeeBean

public class AddEmployeeBean {
    @Valid
    private EmployeeBean employeeBean;
    @NotEmpty
    private List<EmployeeBean> employeeDetails;
           |
           |
         getter- setter  
}

内部Bean - EmployeeBean

public class EmployeeBean {
    @NotEmpty
    private String employeeId;
    @NumberFormat(style = Style.NUMBER)
    private long dept;
    @NumberFormat(style = Style.NUMBER)
    private long salary;
    @NotEmpty
    private String status;
           |
           |
         getter- setter 
}   

0 个答案:

没有答案