Spring Boot Pojo验证

时间:2018-05-08 05:06:04

标签: java spring spring-mvc spring-boot

我目前正在从事项目工作。我有一个类似RtbRequest的pojo类,其中有许多字段,但在类用户需要验证。

public class RtbRequest {
      private String id;
      private Site site;
      private App app;
      private List<String> wseat;
      private List<String> bseat;
}

在此json呼叫中,用户发送可能的组合

  1. id,site,wseat
  2. id,site,bseat
  3. id,app,wseat
  4. id,app,bseat
  5. 如果json同时拥有&#34; Site&#34;和&#34; App&#34;显示错误 如果json同时拥有&#34; bseat&#34;和&#34; wseat&#34;显示错误

    我希望当请求来到mapp所有字段时。可以这样做

1 个答案:

答案 0 :(得分:0)

public class RtbRequest {
private String id;
private Site site;
private App app;
private List<String> wseat;
private List<String> bseat;

@AssertTrue(message = "Your custom message")
public boolean checkValidation(){
    if(site != null && app != null){
        return false;
    }

    //add ur required condition here. if ur request valid return true.
    return false;
}

}