2017-08-31 235 views
0

我面臨一個問題,如何驗證json中的空輸入?春天寧靜的Web服務驗證

這裏是我的代碼:

@RequestMapping(value="/findAdpId/", method=RequestMethod.POST , consumes = "application/json", produces = "application/json") 
    public @ResponseBody String findAdpId(@RequestBody AllCustomerHist customer){ 
     String customerId = customer.getCustomerId(); 
     String srctable = customer.getSrctable(); 
     String message; 

     System.out.println("customer ID = "+customerId); 
     System.out.println("srctable = "+srctable); 

//  List<AllCustomerHist> adpcust = allCustomerHistService.findAdpId(customerId, srctable); 
     BigDecimal adpcust = (BigDecimal) allCustomerHistService.findAdpId(customerId, srctable); 


     JSONObject json = new JSONObject(); 
     json.put("adpId", adpcust); 

     message = json.toString(); 
     return message; 
    } 

我要的是驗證customerIdsrctable是不是空值,這裏是我的DTO

@Entity 
@Table(name="all_customer_hist") 
public class AllCustomerHist { 
    @Column(name = "REFF_FLAG") 
    private String reffFlag; 
    @Column(name = "REFF_ID", precision = 19, scale = 0) 
    private BigDecimal reffId; 
    @Column(name = "LAST_UPDATED_DATE") 
    private Date lastUpdatedDate; 
    @Column(name = "CREATED_DATE") 
    private Date createdDate; 
    @Column(name = "CUSTOMER_ID", length = 255) 
    private String customerId; 
    @Column(name = "NAME", length = 255) 
    private String name; 
    @Column(name = "DOB", length = 255) 
    private String dob; 
    @Column(name = "BIRTH_PLACE", length = 255) 
    private String birthPlace; 
    @Column(name = "GENDER", length = 255) 
    private String gender; 
    @Column(name = "RELIGION", length = 255) 
    private String religion; 
    @Column(name = "EDUCATION", length = 255) 
    private String education; 
    @Column(name = "EMPLOYEE_TYPE", length = 255) 
    private String employeeType; 
    @Column(name = "MARITAL_STATUS", length = 255) 
    private String maritalStatus; 
    @Column(name = "ADDRESS", length = 300) 
    private String address; 
    @Column(name = "CITY", length = 255) 
    private String city; 
    @Column(name = "KECAMATAN", length = 255) 
    private String kecamatan; 
    @Column(name = "KELURAHAN", length = 255) 
    private String kelurahan; 
    @Column(name = "ZIP_CODE", length = 255) 
    private String zipCode; 
    @Column(name = "BPS_CODE", length = 255) 
    private String bpsCode; 
    @Column(name = "ID_TYPE", length = 255) 
    private String idType; 
    @Column(name = "ID_NUMBER", length = 255) 
    private String idNumber; 
    @Column(name = "NPWP", length = 255) 
    private String npwp; 
    @Column(name = "HOME_PHONE_1", length = 255) 
    private String homePhone1; 
    @Column(name = "HOME_PHONE_2", length = 255) 
    private String homePhone2; 
    @Column(name = "HOME_PHONE_3", length = 255) 
    private String homePhone3; 
    @Column(name = "HOME_PHONE_4", length = 255) 
    private String homePhone4; 
    @Column(name = "HOME_PHONE_5", length = 255) 
    private String homePhone5; 
    @Column(name = "OFFICE_PHONE_1", length = 255) 
    private String officePhone1; 
    @Column(name = "OFFICE_PHONE_2", length = 255) 
    private String officePhone2; 
    @Column(name = "OFFICE_PHONE_3", length = 255) 
    private String officePhone3; 
    @Column(name = "OFFICE_PHONE_4", length = 255) 
    private String officePhone4; 
    @Column(name = "OFFICE_PHONE_5", length = 255) 
    private String officePhone5; 
    @Column(name = "MOBILE_PHONE_1", length = 255) 
    private String mobilePhone1; 
    @Column(name = "MOBILE_PHONE_2", length = 255) 
    private String mobilePhone2; 
    @Column(name = "MOBILE_PHONE_3", length = 255) 
    private String mobilePhone3; 
    @Column(name = "MOBILE_PHONE_4", length = 255) 
    private String mobilePhone4; 
    @Column(name = "MOBILE_PHONE_5", length = 255) 
    private String mobilePhone5; 
    @Column(name = "FAX_NUMBER_1", length = 255) 
    private String faxNumber1; 
    @Column(name = "FAX_NUMBER_2", length = 255) 
    private String faxNumber2; 
    @Column(name = "EMAIL_1", length = 255) 
    private String email1; 
    @Column(name = "EMAIL_2", length = 255) 
    private String email2; 
    @Column(name = "EMAIL_3", length = 255) 
    private String email3; 
    @Column(name = "SRCTABLE", length = 255) 
    private String srctable; 
    @Column(name = "ADP_ID", precision = 19, scale = 0) 
    private BigDecimal adpId; 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen") 
    @SequenceGenerator(name="my_entity_seq_gen", sequenceName="SYSTEMID") 
    @Column(name = "SYSTEM_ID", precision = 19, scale = 0) 
    private BigDecimal systemId; 

    //Release 1.2 
    @Column(name = "NAME_RAW", length = 255) 
    private String nameRaw; 
    @Column(name = "CUSTOMER_TITLE", length = 8) 
    private String customerTitle; 
    @Column(name = "GENDERTITLE", length = 8) 
    private String gendertitle; 





    public String getNameRaw() { 
     return nameRaw; 
    } 


    public void setNameRaw(String nameRaw) { 
     this.nameRaw = nameRaw; 
    } 


    public String getCustomerTitle() { 
     return customerTitle; 
    } 


    public void setCustomerTitle(String customerTitle) { 
     this.customerTitle = customerTitle; 
    } 


    public String getGendertitle() { 
     return gendertitle; 
    } 


    public void setGendertitle(String gendertitle) { 
     this.gendertitle = gendertitle; 
    } 


    public String getReffFlag() { 
     return reffFlag; 
    } 


    public void setReffFlag(String reffFlag) { 
     this.reffFlag = reffFlag; 
    } 


    public BigDecimal getReffId() { 
     return reffId; 
    } 


    public void setReffId(BigDecimal reffId) { 
     this.reffId = reffId; 
    } 


    public Date getLastUpdatedDate() { 
     return lastUpdatedDate; 
    } 


    public void setLastUpdatedDate(Date lastUpdatedDate) { 
     this.lastUpdatedDate = lastUpdatedDate; 
    } 


    public Date getCreatedDate() { 
     return createdDate; 
    } 


    public void setCreatedDate(Date createdDate) { 
     this.createdDate = createdDate; 
    } 


    public String getCustomerId() { 
     return customerId; 
    } 


    public void setCustomerId(String customerId) { 
     this.customerId = customerId; 
    } 


    public String getName() { 
     return name; 
    } 


    public void setName(String name) { 
     this.name = name; 
    } 


    public String getDob() { 
     return dob; 
    } 


    public void setDob(String dob) { 
     this.dob = dob; 
    } 


    public String getBirthPlace() { 
     return birthPlace; 
    } 


    public void setBirthPlace(String birthPlace) { 
     this.birthPlace = birthPlace; 
    } 


    public String getGender() { 
     return gender; 
    } 


    public void setGender(String gender) { 
     this.gender = gender; 
    } 


    public String getReligion() { 
     return religion; 
    } 


    public void setReligion(String religion) { 
     this.religion = religion; 
    } 


    public String getEducation() { 
     return education; 
    } 


    public void setEducation(String education) { 
     this.education = education; 
    } 


    public String getEmployeeType() { 
     return employeeType; 
    } 


    public void setEmployeeType(String employeeType) { 
     this.employeeType = employeeType; 
    } 


    public String getMaritalStatus() { 
     return maritalStatus; 
    } 


    public void setMaritalStatus(String maritalStatus) { 
     this.maritalStatus = maritalStatus; 
    } 


    public String getAddress() { 
     return address; 
    } 


    public void setAddress(String address) { 
     this.address = address; 
    } 


    public String getCity() { 
     return city; 
    } 


    public void setCity(String city) { 
     this.city = city; 
    } 


    public String getKecamatan() { 
     return kecamatan; 
    } 


    public void setKecamatan(String kecamatan) { 
     this.kecamatan = kecamatan; 
    } 


    public String getKelurahan() { 
     return kelurahan; 
    } 


    public void setKelurahan(String kelurahan) { 
     this.kelurahan = kelurahan; 
    } 


    public String getZipCode() { 
     return zipCode; 
    } 


    public void setZipCode(String zipCode) { 
     this.zipCode = zipCode; 
    } 


    public String getBpsCode() { 
     return bpsCode; 
    } 


    public void setBpsCode(String bpsCode) { 
     this.bpsCode = bpsCode; 
    } 


    public String getIdType() { 
     return idType; 
    } 


    public void setIdType(String idType) { 
     this.idType = idType; 
    } 


    public String getIdNumber() { 
     return idNumber; 
    } 


    public void setIdNumber(String idNumber) { 
     this.idNumber = idNumber; 
    } 


    public String getNpwp() { 
     return npwp; 
    } 


    public void setNpwp(String npwp) { 
     this.npwp = npwp; 
    } 


    public String getHomePhone1() { 
     return homePhone1; 
    } 


    public void setHomePhone1(String homePhone1) { 
     this.homePhone1 = homePhone1; 
    } 


    public String getHomePhone2() { 
     return homePhone2; 
    } 


    public void setHomePhone2(String homePhone2) { 
     this.homePhone2 = homePhone2; 
    } 


    public String getHomePhone3() { 
     return homePhone3; 
    } 


    public void setHomePhone3(String homePhone3) { 
     this.homePhone3 = homePhone3; 
    } 


    public String getHomePhone4() { 
     return homePhone4; 
    } 


    public void setHomePhone4(String homePhone4) { 
     this.homePhone4 = homePhone4; 
    } 


    public String getHomePhone5() { 
     return homePhone5; 
    } 


    public void setHomePhone5(String homePhone5) { 
     this.homePhone5 = homePhone5; 
    } 


    public String getOfficePhone1() { 
     return officePhone1; 
    } 


    public void setOfficePhone1(String officePhone1) { 
     this.officePhone1 = officePhone1; 
    } 


    public String getOfficePhone2() { 
     return officePhone2; 
    } 


    public void setOfficePhone2(String officePhone2) { 
     this.officePhone2 = officePhone2; 
    } 


    public String getOfficePhone3() { 
     return officePhone3; 
    } 


    public void setOfficePhone3(String officePhone3) { 
     this.officePhone3 = officePhone3; 
    } 


    public String getOfficePhone4() { 
     return officePhone4; 
    } 


    public void setOfficePhone4(String officePhone4) { 
     this.officePhone4 = officePhone4; 
    } 


    public String getOfficePhone5() { 
     return officePhone5; 
    } 


    public void setOfficePhone5(String officePhone5) { 
     this.officePhone5 = officePhone5; 
    } 


    public String getMobilePhone1() { 
     return mobilePhone1; 
    } 


    public void setMobilePhone1(String mobilePhone1) { 
     this.mobilePhone1 = mobilePhone1; 
    } 


    public String getMobilePhone2() { 
     return mobilePhone2; 
    } 


    public void setMobilePhone2(String mobilePhone2) { 
     this.mobilePhone2 = mobilePhone2; 
    } 


    public String getMobilePhone3() { 
     return mobilePhone3; 
    } 


    public void setMobilePhone3(String mobilePhone3) { 
     this.mobilePhone3 = mobilePhone3; 
    } 


    public String getMobilePhone4() { 
     return mobilePhone4; 
    } 


    public void setMobilePhone4(String mobilePhone4) { 
     this.mobilePhone4 = mobilePhone4; 
    } 


    public String getMobilePhone5() { 
     return mobilePhone5; 
    } 


    public void setMobilePhone5(String mobilePhone5) { 
     this.mobilePhone5 = mobilePhone5; 
    } 


    public String getFaxNumber1() { 
     return faxNumber1; 
    } 


    public void setFaxNumber1(String faxNumber1) { 
     this.faxNumber1 = faxNumber1; 
    } 


    public String getFaxNumber2() { 
     return faxNumber2; 
    } 


    public void setFaxNumber2(String faxNumber2) { 
     this.faxNumber2 = faxNumber2; 
    } 


    public String getEmail1() { 
     return email1; 
    } 


    public void setEmail1(String email1) { 
     this.email1 = email1; 
    } 


    public String getEmail2() { 
     return email2; 
    } 


    public void setEmail2(String email2) { 
     this.email2 = email2; 
    } 


    public String getEmail3() { 
     return email3; 
    } 


    public void setEmail3(String email3) { 
     this.email3 = email3; 
    } 


    public String getSrctable() { 
     return srctable; 
    } 


    public void setSrctable(String srctable) { 
     this.srctable = srctable; 
    } 


    public BigDecimal getAdpId() { 
     return adpId; 
    } 


    public void setAdpId(BigDecimal adpId) { 
     this.adpId = adpId; 
    } 


    public BigDecimal getSystemId() { 
     return systemId; 
    } 


    public void setSystemId(BigDecimal systemId) { 
     this.systemId = systemId; 
    } 

    public AllCustomerHist(){} 

    public AllCustomerHist(String reffFlag, BigDecimal reffId, Date lastUpdatedDate, Date createdDate, 
      String customerId, String name, String dob, String birthPlace, String gender, String religion, 
      String education, String employeeType, String maritalStatus, String address, String city, String kecamatan, 
      String kelurahan, String zipCode, String bpsCode, String idType, String idNumber, String npwp, 
      String homePhone1, String homePhone2, String homePhone3, String homePhone4, String homePhone5, 
      String officePhone1, String officePhone2, String officePhone3, String officePhone4, String officePhone5, 
      String mobilePhone1, String mobilePhone2, String mobilePhone3, String mobilePhone4, String mobilePhone5, 
      String faxNumber1, String faxNumber2, String email1, String email2, String email3, String srctable, 
      BigDecimal adpId, BigDecimal systemId, String nameRaw, String customerTitle, String gendertitle) { 
     super(); 
     this.reffFlag = reffFlag; 
     this.reffId = reffId; 
     this.lastUpdatedDate = lastUpdatedDate; 
     this.createdDate = createdDate; 
     this.customerId = customerId; 
     this.name = name; 
     this.dob = dob; 
     this.birthPlace = birthPlace; 
     this.gender = gender; 
     this.religion = religion; 
     this.education = education; 
     this.employeeType = employeeType; 
     this.maritalStatus = maritalStatus; 
     this.address = address; 
     this.city = city; 
     this.kecamatan = kecamatan; 
     this.kelurahan = kelurahan; 
     this.zipCode = zipCode; 
     this.bpsCode = bpsCode; 
     this.idType = idType; 
     this.idNumber = idNumber; 
     this.npwp = npwp; 
     this.homePhone1 = homePhone1; 
     this.homePhone2 = homePhone2; 
     this.homePhone3 = homePhone3; 
     this.homePhone4 = homePhone4; 
     this.homePhone5 = homePhone5; 
     this.officePhone1 = officePhone1; 
     this.officePhone2 = officePhone2; 
     this.officePhone3 = officePhone3; 
     this.officePhone4 = officePhone4; 
     this.officePhone5 = officePhone5; 
     this.mobilePhone1 = mobilePhone1; 
     this.mobilePhone2 = mobilePhone2; 
     this.mobilePhone3 = mobilePhone3; 
     this.mobilePhone4 = mobilePhone4; 
     this.mobilePhone5 = mobilePhone5; 
     this.faxNumber1 = faxNumber1; 
     this.faxNumber2 = faxNumber2; 
     this.email1 = email1; 
     this.email2 = email2; 
     this.email3 = email3; 
     this.srctable = srctable; 
     this.adpId = adpId; 
     this.systemId = systemId; 
     this.nameRaw = nameRaw; 
     this.customerTitle = customerTitle; 
     this.gendertitle = gendertitle; 
    } 
} 

我的目標是,當此JSON拋出:

{ 
    "srctable":null, 
    "customerId":null 
    } 

它應該返回消息,通知消費者t hat srctable和customerId不能爲null,但我不想在我的dto中添加@NotNull,因爲customerId和srctable可能對其他進程爲空。

謝謝你,等待你的幫助:d

+0

在這種情況下,您總是可以使用'if-else'條件! – sam

+0

1.你的粘貼代碼包含了太多的數據 - 你不需要整個AllCustomerHist類來說明你的問題。 2.將您的數據庫模型綁定到API模型幾乎總是一個非常糟糕的選擇。 3.你想要的東西實際上是一個域級驗證,它不應該在控制器內執行。 –

+0

@RafalG。實際上,我有模型層,dao層和服務層,我真的沒有把它「綁定你的數據庫模型到API模型幾乎總是一個非常糟糕的選擇」,你能解釋它更詳細的信息嗎? 還有什麼是域級驗證?以及應該在哪裏執行?因爲請求是從控制器 – galih

回答

1

它的時間來介紹DTO(數據傳輸對象),並在控制器中使用它。猜猜你不需要一些字段,例如實體的lastUpdatedDate不在搜索邏輯中使用。

您需要添加必要字段的自定義POJO,並使用@NotNull和所有其他必要的驗證檢查對其進行註釋。您也可能需要轉換器以在必要時將DTO轉換爲實體。

+0

處理的,不是所有這個方法所需的字段,我應該創建新的dto嗎? 我真的不知道它的部分添加必要的字段添加自定義pojo和註釋@NotNull和所有其他必要的驗證檢查。您也可能需要轉換器以在必要時將DTO轉換爲實體。 你能解釋一下它的更多細節嗎?謝謝 – galih

+0

是的。好處 - 減少信息傳遞,拆分模型(實體)和控制器(DTO),任何驗證你需要(額外轉換可能包括例如從字符串表示到類的轉換。例如,對於日期字段,我可以傳遞值「now」或「yesterday」並添加綁定邏輯轉換爲Date類)。 – StanislavL

+0

你有什麼好的參考嗎?我在春天很新的java – galih

0

使用@Validjavax.validation.Valid@NotNull註釋。

此外,我想建議一個博客爲休息API驗證here

+0

我不想在我的實體對象中使用@Notnull anotation,因爲並非所有進程都需要這些字段爲空/不爲空 反正謝謝分享:) – galih