Spring HandlerMethodArgumentResolver not executing
OK I worked out that Spring was already resolving the Principal object in my above example and so my argument resolver was not kicking in. I had been lazy and added the @CurrentUser annotation to an existing parameter.
So I changed my example
@RequestMapping(method = RequestMethod.POST, value = "/update")
public ModelAndView update(@RequestParam MultipartFile background, @CurrentUser Principal principal) {
...
}
to use my User model class
@RequestMapping(method = RequestMethod.POST, value = "/update")
public ModelAndView update(@RequestParam MultipartFile background, @CurrentUser User user) {
...
}
and now it works!
If anybody ever wants to prioritize custom handlers over default handlers added by spring, here's a snippet that does it for me, I do this in a @Configuration
file
private @Inject RequestMappingHandlerAdapter adapter;
@PostConstruct
public void prioritizeCustomArgumentMethodHandlers () {
List<HandlerMethodArgumentResolver> argumentResolvers =
new ArrayList<> (adapter.getArgumentResolvers ());
List<HandlerMethodArgumentResolver> customResolvers =
adapter.getCustomArgumentResolvers ();
argumentResolvers.removeAll (customResolvers);
argumentResolvers.addAll (0, customResolvers);
adapter.setArgumentResolvers (argumentResolvers);
}