Custom validator issue No bean introspection present. Please add @Introspected to the class and ensure Micronaut annotation processing is enabled not working Micronaut 2.2.1

@Retention(RetentionPolicy.RUNTIME)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Constraint(validatedBy = { })
public @interface LastProductIdRequired {
    String message() default "Product id is required for pagination if current page is greater then 0";
}

Factory

@Factory
public class LastProductIdRequiredFactory {
    @Singleton
    ConstraintValidator<LastProductIdRequired, PaginationViewModel> lastProductIdRequired() {
        return (value, annotationMetadata, context) ->
                value != null && (value.getCurrentPage() > 0 && value.getLastProductId() != null);
    }
}

POJO

@Introspected
public class PaginationViewModel {
    int pageSize = 10;
    int total = 0;
    int currentPage = 0;
    int totalPage = 0;
    @LastProductIdRequired
    String lastProductId;
    ....
}


@Introspected
public class ProductSearchCriteria extends PaginationViewModel{

}

Controller

@Controller("/product")
@Validated
public class ProductController {
@Get(value = "/{?searchCriteria*}")
public Maybe<FindProductViewModel> find(@Valid ProductSearchCriteria searchCriteria) {
        LOG.info("Controller --> Finding all the products");
        return iProductManager.find(searchCriteria);
    }
}

Error

{
    "message": "searchCriteria: Cannot validate view.model.product.ProductSearchCriteria. No bean introspection present. Please add @Introspected to the class and ensure Micronaut annotation processing is enabled",
    "_links": {
        "self": {
            "href": "/api/v1/product",
            "templated": false
        }
    }
}
1

There are 1 answers

0
Md. Shahariar Hossen On

You can run such kind of method, and provide ProductSearchCriteria as argument

private Set<String> getIntrospectedProperties(Class<?> productSearchCriteria) {
final BeanIntrospection<?> introspection =
BeanIntrospection.getIntrospection(productSearchCriteria);
return Arrays.stream(introspection.getPropertyNames()).collect(Collectors.toSet());
}