spring-framework
spring-framework copied to clipboard
MethodArgumentNotValidException from spring-web requires spring-context to compile
Affects: org.springframework:spring-web:5.3.23
Trying to update from 5.2 to 5.3.23 lead us to an interesting issue, where a usage of MethodArgumentNotValidException would not compile. A minimal reproduction would be a gradle project as follows:
excerpt from build.gradle:
dependencies {
implementation 'org.springframework:spring-web:5.3.23'
}
A java class:
import org.springframework.web.bind.MethodArgumentNotValidException;
public class Main {
public static void main(String [] args) {
throw new MethodArgumentNotValidException(null, null);
}
}
This fails to compile with following error:
error: cannot access BindException
throw new MethodArgumentNotValidException(null, null);
^
class file for org.springframework.validation.BindException not found
This is because MethodArgumentNotValidException (from spring-web) extends org.springframework.validation.BindException (from spring-context), but the pom.xml that gradle sees does not specify this compile time dependency: https://search.maven.org/artifact/org.springframework/spring-web/5.3.23/jar
Only spring-beans and spring-core are specified as compile time dependencies (thus classes from these are present.)
This change of MethodArgumentNotValidException extending BindException was discussed in issues #23107 #26219
I think the correct behaviour here would be to mark spring-context as a compile time dependency for spring-web as that would play along with various build systems better.