2

So am doing spring and hibernate course and got to @NotNull and @size chapter. I implemented hibernate jar files 1,2,3 4,5,6,7 and I don't get any errors in code, but @NotNull and @Size does not work, There is another post about this problem and I saw no solution there so I had to make new post.

Hibernate validator I implement is hibernate-validator-7.0.3.Final and spring am using is spring-framework-5.3.9

   package com.luv2code.springdemo.mvc;
    
    import jakarta.validation.constraints.NotNull;
    import jakarta.validation.constraints.Size;
    
    public class Customer {
        
        private String firstName;
        
        @Size(min = 1, message ="is required")
        @NotNull( message ="is required")
        private String lastName;
    
        
        public Customer() {
            
        }
    
        public String getFirstName() {
            return firstName;
        }
    
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
    
        public String getLastName() {
            return lastName;
        }
    
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        
    }

/

<%@ taglib prefix= "form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>

<title> Customer registration form</title>

<style>
    .error{color:red}
</style>


</head>
<body>
        <form:form action ="processForm" modelAttribute = "customer">
        
        
            First name: <form:input path="firstName" />
            
            <br><br>
            
            Last name: <form:input path="lastName"/>
            <form:errors path= "lastName" cssClass = "error"/>
            
            <br><br>
            
            <input type= "submit" value ="Submit" />
        
        </form:form>
    
    
</body>

</html>

/

package com.luv2code.springdemo.mvc;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.validation.Valid;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    
    @RequestMapping("/showForm")
    public String showForm(Model theModel) {
        
        theModel.addAttribute("customer", new Customer());
        
        return "customer-form";
    }
    
    @RequestMapping("/processForm")
    public String processForm(@Valid @ModelAttribute("customer") Customer theCustomer,
            BindingResult theBindingResult) {
        
        if(theBindingResult.hasErrors()) {
            
            return "customer-form";
        } else {
            return "customer-confirmation";
        }
    }
    
}
Adriaan
  • 17,741
  • 7
  • 42
  • 75
skeper
  • 33
  • 5
  • How are the dependencies being used? Via maven, gradle or other? – pringi Mar 07 '22 at 10:10
  • @pringi You mean this? https://imgur.com/a/dUtu9gd , not via maven,grandle – skeper Mar 07 '22 at 10:23
  • The problem is Hibernate Validator 7, which is a JakartaEE implementation and **not** a JavaEE implementation of the validation API. Use Hibernate Validator 6 which is compatible with Spring 5. For JakartaEE you need Spring 6 which is still in development at this moment. – M. Deinum Mar 07 '22 at 10:46
  • @M.Deinum ok so I removed hibernate-validator-7.0.3.Final , hibernate-validator-annotation-processor-7.0.3.Final and hibernate-validator-cdi-7.0.3.Final, and added Spring Boot Starter Validation ( https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation/2.6.3 ) , restarted everything and tried it again, and it still doesn't work, I must be doing something wrong or something is messed up, I have no clue – skeper Mar 07 '22 at 10:49
  • @M.Deinum it worked, omg thank you, that was the problem whole time, you are magician man – skeper Mar 07 '22 at 11:18
  • Please do not add answers to the question body itself. Instead, you should add it as an answer. [Answering your own question is allowed and even encouraged](https://stackoverflow.com/help/self-answer). – Adriaan Feb 08 '23 at 14:16

3 Answers3

0

I have encountered the same problem and after trying many solutions, what worked for me was a simple modification without configuring or modifying anything else.

I only had to put the @field decorator in the properties of the class to validate.

public class Customer {
        
        private String firstName;
        
        @field:NotNull( message ="is required")
        @Size(min = 1, message ="is required")
        private String lastName;

        ...

}
pipegama
  • 1
  • 1
-2

try to use @NotNull @NotBlank @Size in this sequence

If you want spaces as valid string then use @NotEmpty

KP-
  • 32
  • 3
-2

I had similar issue.

Replacing:

@jakarta.validation.constraints.NotNull

with

@org.hibernate.validator.constraints.NotBlank

and

@jakarta.validation.constraints.Size

with

@org.hibernate.validator.constraints.Length

helped me

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
finca
  • 1
  • I tried what you said but looks like i dont have constraints.NotBlank and constraints.Length Libary, ( https://imgur.com/a/uG9dQbc ), i googled it a bit and found it's the same JAR files i already have imported, https://jar-download.com/artifacts/org.hibernate.validator, is it some other JAR files? – skeper Mar 06 '22 at 17:18
  • Problem is that spring doesn't support JakartaEE (yet) so using a validator from that (hibernate validator 7) isn't going to work. Changing the annotation won't help either. – M. Deinum Mar 07 '22 at 10:45