Search code examples
spring-boothibernatevalidationjavabeansconstraintviolationexception

Hibernate method validation NOT always working


Why is Hibernate's validation - ConstraintViolationException - NOT thrown in main() of a SpringBoot app (SpringBoot's latest version) with spring-boot-starter-web:

@Validated
@SpringBootApplication
public class Application {
public static void main(String[] args) {
   SpringApplication.run(Application.class, args);
   someService.doStuff(new Item(null);  // WHY NOT THROWN????????!!!!!! 
   // Expecting ConstraintViolationException: doStuff.item.content: must not be null
}}
// ----------------------

public class Item {
    @NotNull
    String content;  // to be validated
   //constructor, getter, setter
}

@Validated
@Service
public class SomeService {
    void doStuff(@Valid Item item) {} // should break for Item's content = null
}

Strangely enough, in other cases Hibernate validation is working as expected for the same method call:

  1. ConstraintViolationException is thrown when I put the invalid call in a controller's contructor:
public SomeController(SomeService someService){
    this.someService = someService;
    someService.doStuff(new Item(null); // throws ConstraintViolationException  
}
  1. Also as expected, ConstraintViolationException is thrown when I put the invalid call in a constructor method and call the endpoint in a test or Postman
@GetMapping("item")
public String item() {
    someService.doStuff(new Item(null); // throws ConstraintViolationException
    return "You never get here.";
}

Solution

  • Not sure how are you getting someService instance in Application, but the following code works for me (every class in a different file):

    @AllArgsConstructor
    @Getter
    @Setter
    public class Item {
    
      @NotNull
      String content;
    }
    
    
    
    @Validated
    @Service
    public class SomeService {
    
      public void doStuff(@Valid Item item) {
        System.out.println(format("Item.content = %s", item.getContent()));
      }
    }
    
    
    
    @SpringBootApplication
    public class TestingPurposeApplication {
    
      public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(TestingPurposeApplication.class, args);
        SomeService someService = context.getBean(SomeService.class);
        someService.doStuff(new Item(null));
      }
    }
    

    The result:

    ConstraintViolationException

    Use:

    ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args);
    MyClass myInstance = context.getBean(MyClass.class);
    

    Is the suitable way to get a component managed by Spring in main method.