Search code examples
kotlinbean-validationmicronaut

Micronaut - How to inject Hibernate validator


I am using the Hibernate validation mechanism in my Micronaut project as the documentation suggests.

Unfortunately, I need to react to validation errors manually and handle the errors with specific exceptions. Thus, I can not use the recommended way of validating method arguments

fun doStuff(@Valid argument: MyArgument): ... {...}

I want to inject the Validator to my class, call it and process the results myself. In Spring this was easily doable. Micronaut unfortunately tells me it does not know how to provide a validator:

Message: No bean of type [javax.validation.Validator] exists

My class looks like this

import javax.validation.Validator

@Controller
class MyController(val validator: Validator) {...}

Am I missing something? I read somewhere that there should be a default validation bean factory available as I am using Micronaut v1.1.0.

Thanks.


Solution

  • You can inject a javax.validation.ValidatorFactory that will delegate to whatever implementation you have on the classpath.

    Starting in 1.2.0 (not released yet), you will be able to inject a io.micronaut.validation.validator.Validator that is Micronaut's implementation of validation that doesn't use reflection.