Spring Framework Version = 5.2.9.RELEASE Spring Boot version = 2.3.4.RELEASE
Customize a String to uppercase annotation @UpperCase
, Implement the HandlerMethodArgumentResolver
interface
@Slf4j
public class MethodArgumentUpperCaseResolver implements HandlerMethodArgumentResolver {
public MethodArgumentUpperCaseResolver() {
log.debug("{}", this.getClass().getName());
}
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(UpperCase.class) != null;
}
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
UpperCase upperCase = parameter.getParameterAnnotation(UpperCase.class);
return webRequest.getParameter(upperCase.value()).toUpperCase();
}
}
and add resolvers.add(getMethodArgumentUpperCaseResolver());
When the annotation is used in the controller, it does not work
@GetMapping
public ResponseParam query(@RequestParam(name = "queryType", defaultValue = "APP") @UpperCase QueryType queryType) {
}
Comment From: ToQuery
Receive class type
@Getter
public enum QueryType {
FILTER("FILTER"),
APP("APP");
private final String remark;
QueryType(String remark) {
this.remark = remark;
}
}
Maybe because the receiving param class is an enum ?
Comment From: wilkinsona
Thanks for the report.
If you're looking for some help figuring out how to do something, then I'm afraid that this isn't the right place. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.
If you believe you've found a bug in Spring Boot, then can you please provide some more details as it's not clear to me what you believe the bug to be. If you would like us to spend some time investigating a suspected bug, please spend some time providing a complete yet minimal sample that reproduces the problem. This should be something that we can run without alteration to reproduce the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: ToQuery
like this ?
the repository https://github.com/ToQuery/spring-boot-bug
when requet http://localhost:8080/?queryType=app
have an error
2020-09-23 16:57:30.412 WARN 17086 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.example.demo.QueryType'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @com.example.demo.UpperCase com.example.demo.QueryType] for value 'app'; nested exception is java.lang.IllegalArgumentException: No enum constant com.example.demo.QueryType.app]
hope String 'app' to convert 'APP' , is not working
It was usable in the previous version, but it can’t be used now, suspected to be a bug
Comment From: wilkinsona
Thanks for the sample.
The custom argument resolver isn't being called because queryType
is annotated with @RequestParam
and the RequestParamMethodArgumentResolver
is matched before any custom resolvers are considered. I believe this has always been the case with Spring MVC so I cannot explain how it was usable in a previous version.
If you remove @RequestParam(name = "queryType", defaultValue = "APP")
then your argument resolver is called. It doesn't work correctly as the value of @UpperCase
is an empty string to the parameter lookup returns null
, but that is due to a mistake in the resolver rather than a problem with Spring Boot or Spring Framework.
I'm going to close this issue as I cannot see any sign of a problem with Spring Boot. If you can demonstrate your argument resolver working when annotated with both @UpperCase
and @RequestParam
with one version and failing with another, please raise the matter with the Spring Framework team so that they can investigate the change in Spring MVC's behaviour.