データベースから取得した動的DTO検証構成
1. 概要
このチュートリアルでは、データベースから取得した正規表現を使用してフィールド値と照合するカスタム検証アノテーションを作成する方法を見ていきます。
基本実装としてHibernateValidatorを使用します。
2. Mavenの依存関係
開発には、次の依存関係が必要になります。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.0</version>
</dependency>
spring-boot-starter-thymeleaf 、 spring-boot-starter-data-jpa の最新バージョンは、MavenCentralからダウンロードできます。
3. カスタム検証アノテーション
この例では、データベースから取得した正規表現に対して値を検証する@ContactInfoというカスタムアノテーションを作成します。 次に、この検証をCustomerというPOJOクラスのcontactInfoフィールドに適用します。
データベースから正規表現を取得するために、これらをContactInfoExpressionエンティティクラスとしてモデル化します。
3.1. データモデルとリポジトリ
idフィールドとcontactInfoフィールドを使用してCustomerクラスを作成しましょう。
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String contactInfo;
// standard constructor, getters, setters
}
次に、 ContactInfoExpression クラスを見てみましょう。このクラスは、patternというプロパティに正規表現の値を保持します。
@Entity
public class ContactInfoExpression {
@Id
@Column(name="expression_type")
private String type;
private String pattern;
//standard constructor, getters, setters
}
次に、Spring Dataに基づくリポジトリインターフェイスを追加して、ContactInfoExpressionエンティティを操作します。
public interface ContactInfoExpressionRepository
extends Repository<ContactInfoExpression, String> {
Optional<ContactInfoExpression> findById(String id);
}
3.2. データベースの設定
正規表現を格納するために、次の永続性構成を持つH2インメモリデータベースを使用します。
@EnableJpaRepositories("com.baeldung.dynamicvalidation.dao")
@EntityScan("com.baeldung.dynamicvalidation.model")
@Configuration
public class PersistenceConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2)
.addScript("schema-expressions.sql")
.addScript("data-expressions.sql")
.build();
return db;
}
}
上記の2つのスクリプトは、スキーマを作成し、データをcontact_info_expressionテーブルに挿入するために使用されます。
CREATE TABLE contact_info_expression(
expression_type varchar(50) not null,
pattern varchar(500) not null,
PRIMARY KEY ( expression_type )
);
data-expressions.sql スクリプトは、タイプ email 、 phone、、およびwebsiteを表す3つのレコードを追加します。 これらは、値が有効な電子メールアドレス、有効な米国の電話番号、または有効なURLであることを検証するための正規表現を表します。
insert into contact_info_expression values ('email',
'[a-z0-9!#$%&*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?')
insert into contact_info_expression values ('phone',
'^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$')
insert into contact_info_expression values ('website',
'^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$')
3.3. カスタムバリデーターの作成
実際の検証ロジックを含むContactInfoValidatorクラスを作成しましょう。 Java検証仕様のガイドラインに従って、クラスはConstraintValidatorインターフェースを実装し、 isValid()メソッドをオーバーライドします。
このクラスは、現在使用されている連絡先情報のタイプ( email 、 phone、、または website )の値を取得します。これは、というプロパティに設定されています。 contactInfoType 、次にそれを使用してデータベースから正規表現の値を取得します。
public class ContactInfoValidator implements ConstraintValidator<ContactInfo, String> {
private static final Logger LOG = Logger.getLogger(ContactInfoValidator.class);
@Value("${contactInfoType}")
private String expressionType;
private String pattern;
@Autowired
private ContactInfoExpressionRepository expressionRepository;
@Override
public void initialize(ContactInfo contactInfo) {
if (StringUtils.isEmptyOrWhitespace(expressionType)) {
LOG.error("Contact info type missing!");
} else {
pattern = expressionRepository.findById(expressionType)
.map(ContactInfoExpression::getPattern).get();
}
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (!StringUtils.isEmptyOrWhitespace(pattern)) {
return Pattern.matches(pattern, value);
}
LOG.error("Contact info pattern missing!");
return false;
}
}
contactInfoType プロパティは、 application.properties ファイルで、 email 、 phone 、または
contactInfoType=email
3.4. カスタム制約アノテーションの作成
それでは、カスタム制約のアノテーションインターフェイスを作成しましょう。
@Constraint(validatedBy = { ContactInfoValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface ContactInfo {
String message() default "Invalid value";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
3.5. カスタム制約の適用
最後に、CustomerクラスのcontactInfoフィールドに検証アノテーションを追加しましょう。
public class Customer {
// ...
@ContactInfo
@NotNull
private String contactInfo;
// ...
}
4. SpringControllerとHTMLフォーム
検証アノテーションをテストするために、@Validアノテーションを使用してCustomerオブジェクトの検証をトリガーするSpringMVCリクエストマッピングを作成します。
@PostMapping("/customer")
public String validateCustomer(@Valid Customer customer, BindingResult result, Model model) {
if (result.hasErrors()) {
model.addAttribute("message", "The information is invalid!");
} else {
model.addAttribute("message", "The information is valid!");
}
return "customer";
}
Customer オブジェクトは、HTMLフォームからコントローラーに送信されます。
<form action="customer" method="POST">
Contact Info: <input type="text" name="contactInfo" /> <br />
<input type="submit" value="Submit" />
</form>
<span th:text="${message}"></span>
すべてをまとめると、アプリケーションをSpringBootアプリケーションとして実行できます。
@SpringBootApplication
public class DynamicValidationApp {
public static void main(String[] args) {
SpringApplication.run(DynamicValidationApp.class, args);
}
}
5. 結論
この例では、データベースから正規表現を動的に取得し、それを使用して注釈付きフィールドを検証するカスタム検証アノテーションを作成する方法を示しました。
この例の完全なソースコードは、GitHubのにあります。