[JAVA] Hivernate Validator does not work in WAS Liberty environment

Overview

When I tried to validate with Controller using Hibernate Validator in WASLiberty environment, only the fields annotated with Hibernate Validator were not checked. It seems that some people are having trouble with similar events even though they have been resolved, so I will leave it as a memo.

environment

This event

First of all, from a simple configuration. 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>Hibernate Validator validation</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>Hibernate Validator validation</h1>
				<p>Hibernate Validator verification demo page.</p>
			</div>
			<div class="panel panel-success">
				<div class="panel-heading">Validation check</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">Run</button>

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

In this state, deploy to WAS Liberty and try it. スクリーンショット 2019-10-03 21.19.04.png
For some reason, only Not Empty is executed. .. ..

Try it with tomcat

Try putting the same application on Tomcat. スクリーンショット 2019-10-03 21.20.57.png Yeah, it works. .. .. Why orz

How to respond

The problem seems to be that the Hibernate Validator is not recognized by the WASLiberty class loader. By adding the following validation.xml to META-INF of the target project so that Hibernate Validator is used preferentially, validation was executed successfully. 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

Finally

For reference, upload this verification application to Github. The event did not hit easily and I managed to solve it through trial and error, but since I spent a lot of time on this investigation, I would like people who are stuck with similar events to refer to it.

Recommended Posts

Hivernate Validator does not work in WAS Liberty environment
Sidekiq-limit_fetch does not work
[Ruby on Rails] credential does not work in production environment even though I saved production.key
Pay.JP Solution when it can be done locally but it does not work well in the production environment
eclipse tab width change does not work.
JavaScript (vanilla) does not respond in Rails.
MySQL container does not start in Docker
.sql file does not run in docker-compose
Creating a Servlet in the Liberty environment
Image is not displayed in production environment
File manager does not start when logging in again in Docker's LXDE desktop environment
Code written in Java-TeraPad starting from beginner does not work due to error
Does the escape sequence (\) not work? (for Mac)
Solution for NetBeans 8.2 not working in Java 9 environment
When @Transactional of Spring Boot does not work
"Do not show again" check does not work in the warning dialog when starting applet