spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 355 forks source link

Spring MVC REST controller not working when @Validated annotation is present on interface #1674

Closed gschlueter-jaconi closed 2 years ago

gschlueter-jaconi commented 2 years ago

When putting a @Validated annotation on an interface and implementing that interface (with at least one method) in a Spring MVC REST controller, no mapping for the controller is created and a 404 is returned.

A simple example looks like this:

package io.jaconi.nativevalidatedcontroller;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

@Validated
public interface ValidatedInterface {

    @GetMapping("/interface")
    default ResponseEntity<String> get() {
        return ResponseEntity.ok("Validated Interface");
    }
}

and the implementation:

package io.jaconi.nativevalidatedcontroller;

import org.springframework.stereotype.Controller;

@Controller
public class ValidatedInterfaceController implements ValidatedInterface {

}

An example project can be found here: https://github.com/jaconi-io/native-validated-controller.

Running ./gradlew test succeeds while ./gradlew nativeTest fails for 2 of the 4 tests. When building the native image and running it, mappings for 2 of the 4 controllers will be created.

jfiedler-jaconi commented 2 years ago

This code structure is somewhat common if MVC controllers are generated based on OpenAPI specs via the OpenAPI generator plugins. It is therefore not easy to workaround this problem (by avoiding interfaces or putting annotations at a different method).

sdeleuze commented 2 years ago

This should be supported with upcoming Spring Framework 6 and Spring Boot 3 where most development effort is done these days. Validation support is still WIP, I will make sure the interface level @Validated is supported.