LoginSignup
3
1

More than 3 years have passed since last update.

WAS Liberty環境でHivernate Validatorが動作しない問題

Last updated at Posted at 2019-10-03

概要

WASLiberty環境でHibernateValidatorを使用しControllerでバリデーションを行おうとしたところ、HibernateValidatorのアノテーションを付与したフィールドだけチェック処理が行われなかった。
なんとか解消したものの同じような事象に困ってる人がいそうなので、メモとして残しておく。

環境

  • Spring Boot 2.1.5.RELEASE
    ※今回はSpringBootを利用したが、通常のSpringプロジェクトでも同様の事象が発生。
  • Hibernate-Validator 5.4.3.Final
  • WAS Liberty 19.0.0.9
  • Tomcat8(検証用)

今回の事象

まずは簡単な構成から。

Controller

package com.atu496.sample.pl.controller;

@Controller
public class MainController {

    @GetMapping("/")
    public ModelAndView get(@ModelAttribute SampleForm form, ModelAndView mav) {
        mav.addObject("form", form);
        mav.setViewName("index");
        return mav;
    }

    @PostMapping("/exe/validation")
    public String exe(@ModelAttribute @Valid SampleForm form, BindingResult result, RedirectAttributes attribute) {
        if (result.hasErrors()) {ß
            for (ObjectError error : result.getAllErrors()) {
                String fieldErrors = ((FieldError) error).getField();
                attribute.addFlashAttribute(fieldErrors + "errormsg", error.getDefaultMessage());
            }
        }
        return "redirect:/";
    }
}

Form

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;

import lombok.Data;

@Data
public class SampleForm {

    @Length(min = 8, max = 20)
    private String length;

    @NotBlank
    private String notblank;

    @NotEmpty
    private String notempty;

    @Email
    private String email;
}

Thymeleaf

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>HibernateValidator検証</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
    crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
    integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
    crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
    integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
    crossorigin="anonymous"></script>

</head>
<body>
    <div class="container">
        <div class="row">
            <div class="jumbotron">
                <h1>HibernateValidator検証</h1>
                <p>HibernateValidator検証のデモページです。</p>
            </div>
            <div class="panel panel-success">
                <div class="panel-heading">バリデーションチェック</div>
                <div class="panel-body">
                    <form method="post" action="#" th:action="@{/exe/validation}">
                        <p>
                            @Length:<input type="text" name="length" /><span
                                th:text="${lengtherrormsg}"></span>
                        </P>
                        <p>
                            @Email:<input type="text" name="email" /><span
                                th:text="${emailerrormsg}"></span>
                        </P>
                        <p>
                            @NotEmpty:<input type="text" name="notnull" /><span
                                th:text="${notemptyerrormsg}"></span>
                        </P>
                        <p>
                            @NotBlank:<input type="text" name="notblank" /><span
                                th:text="${notblankerrormsg}"></span>
                        </p>
                        <button type="submit" class="btn btn-default">実行</button>

                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

この状態でWASLibertyにデプロイして試してみる。

スクリーンショット 2019-10-03 21.19.04.png

なぜかNotEmpty以外が実行されない。。。

Tomcatでやってみる

同じアプリケーションをTomcatにのせてみる。

スクリーンショット 2019-10-03 21.20.57.png
うん、うまくいく。。。なぜだorz

対応方法

問題はどうやらWASLibertyのクラスローダーにHibernateValidatorが認識されていないことが原因らしい。

対象プロジェクトのMETA-INFに下記のvalidation.xmlを追加することでHibernateValidatorを優先して使用するようにしたところ、無事バリデーションが実行された。

validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration http://www.jboss.org/xml/ns/javax/validation/configuration/validation-configuration-1.0.xsd">

    <default-provider>org.hibernate.validator.HibernateValidator</default-provider>
</validation-config>

スクリーンショット 2019-10-03 21.15.58.png

最後に

参考程度に今回の検証用アプリをGithubにアップしておく。
事象がなかなかヒットせず試行錯誤を重ねてなんとか解決できたが、この調査に結構時間を使ってしまったので同じような事象に詰まっている人は参考にしてほしい。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1