Skip to content

Custom Constraint Validator not work with native image #29823

@burl21

Description

@burl21

In a new project generated by Spring Initializr, I created a RestController with custom ConstraintValidator and it works fine in a JVM setup:

@Validated
@RestController
@RequestMapping("/")
public class HelloController {

	@GetMapping("hello")
	public String hello(@RequestParam @Exists String name) {
		return "Hello %s.".formatted(name);
	}

}
@Component
public class ExistsValidator implements ConstraintValidator<Exists, String> {
	private final DataService service;

	public ExistsValidator(DataService service) {
		this.service = service;
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
		return service.exists(value);
	}
}

However, for the native image it doesn't work with the error message:

ERROR 19797 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.nativevalidation.ExistsValidator': Failed to instantiate [com.example.nativevalidation.ExistsValidator]: No default constructor found] with root cause

java.lang.NoSuchMethodException: com.example.nativevalidation.ExistsValidator.<init>()
...

In reflect-config.json you can see the configuration of ExistsValidator:

 {
    "name": "com.example.nativevalidation.ExistsValidator",
    "queriedMethods": [
      {
        "name": "<init>",
        "parameterTypes": [
          "com.example.nativevalidation.DataService"
        ]
      }
    ]
  }

I found no issues with the jakarta.validation.constraints.* annotations.
Is it a limitation with native-image to be able to inject a bean into a ConstraintValidator ?

Spring Boot v3.0.1

Metadata

Metadata

Assignees

Labels

theme: aotAn issue related to Ahead-of-time processingtype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions