- Spring 5.0 Projects
- Nilang Patel
- 239字
- 2021-07-02 12:34:56
Using Hibernate Validator to add validations
There are a few checks we need to add to our model classes so that the data being sent from the UI is not invalid. For this, we will make use of Hibernate Validator. You are required to add the Hibernate dependency as follows:
<properties>
<java.version>1.8</java.version>
<lombok.version>1.16.18</lombok.version>
<hibernate.validator.version>6.0.2.Final</hibernate.validator.version>
</properties>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
Now go back to com.nilangpatel.worldgdp.model.Country and update it with the following:
@Data public class Country {
@NotNull @Size(max = 3, min = 3) private String code;
@NotNull @Size(max = 52) private String name;
@NotNull private String continent;
@NotNull @Size(max = 26) private String region;
@NotNull private Double surfaceArea;
private Short indepYear;
@NotNull private Long population;
private Double lifeExpectancy;
private Double gnp;
@NotNull private String localName;
@NotNull private String governmentForm;
private String headOfState;
private City capital;
@NotNull private String code2;
}
Next is to update the com.nilangpatel.worldgdp.model.City class in a similar way, as follows:
@Data public class City {
@NotNull private Long id;
@NotNull @Size(max = 35) private String name;
@NotNull @Size(max = 3, min = 3) private String countryCode;
private Country country;
@NotNull @Size(max = 20) private String district;
@NotNull private Long population;
}
And finally, update com.nilangpatel.worldgdp.model.CountryLanguage class as well, as follows:
@Data
public class CountryLanguage {
private Country country;
@NotNull private String countryCode;
@NotNull @Size(max = 30) private String language;
@NotNull @Size(max = 1, min = 1) private String isOfficial;
@NotNull private Double percentage;
}
推薦閱讀
- 智慧城市:大數(shù)據(jù)、互聯(lián)網(wǎng)時(shí)代的城市治理(第4版)
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級(jí)的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- CorelDRAW X6 中文版圖形設(shè)計(jì)實(shí)戰(zhàn)從入門到精通
- C++黑客編程揭秘與防范
- INSTANT PhpStorm Starter
- 網(wǎng)絡(luò)故障現(xiàn)場(chǎng)處理實(shí)踐(第4版)
- Django 2 by Example
- 網(wǎng)絡(luò)安全技術(shù)與解決方案(修訂版)
- 物聯(lián)網(wǎng)時(shí)代
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- Windows Server 2012 Hyper-V虛擬化管理實(shí)踐
- 紅藍(lán)攻防:構(gòu)建實(shí)戰(zhàn)化網(wǎng)絡(luò)安全防御體系
- Microsoft Power Platform Enterprise Architecture
- 無(wú)線傳感器網(wǎng)絡(luò)定位技術(shù)
- 中國(guó)互聯(lián)網(wǎng)發(fā)展報(bào)告2021