First of all, this code won't compile since you have redeclared a variable within a scope. Moreover, if I understood the question correctly, if you refactor the given snippet of code (so that it compile), spring will throw an exception when notice the first required parameter is missing just like jvm throws NullPointerException in analogous situation with information about exception only in line with exception:
String a = null;
String b = null;
a.length(); //NullPointerException exception thrown
b.length();
You have to create your custom validator checking if parameters were given in request and if not, throw an appropriate exception. Something like:
void validate(String param1, String param2) {
Stream.of(param1, param2).filter(Objects::nonNull).findAny().orElseThrow(() -> new IllegalArgumentException("param1 and param2 are missing"));
Optional.ofNullable(param1).orElseThrow(() -> new IllegalArgumentException("param1 is missing"));
Optional.ofNullable(param2).orElseThrow(() -> new IllegalArgumentException("param2 is missing"));
}
with an exception you'd like to throw. If you want to do it this way, set values of "required" flags to false.