您好我正在尝试创建自定义HandlerMethodArgumentResolver。我面临的问题是它的方法永远不会被调用。这就是我所做的:
我的论点解析器
public class ExternalUserDetailsArgumentResolver implements HandlerMethodArgumentResolver {
public boolean supportsParameter(MethodParameter parameter) {
return (parameter.getParameterAnnotation(ExternalUserDetails.class) != null);
}
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
ExternalUserDetails userDetailsAnnotation = parameter.getParameterAnnotation(ExternalUserDetails.class);
if (userDetailsAnnotation != null) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
ExternalUser userDetails = (ExternalUser) auth.getPrincipal();
return userDetails;
}
return null;
}
}
注释
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExternalUserDetails {
}
配置(使用没有.xml配置)
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "my.base.package" })
public class MVCConfig extends WebMvcConfigurationSupport {
@Bean
MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}
@Bean(name = "validator")
public Validator getStandardValidator() {
return new org.springframework.validation.beanvalidation.LocalValidatorFactoryBean();
}
@Bean(name = "userDetailsArgumentResolver")
public ExternalUserDetailsArgumentResolver userDetailsArgumentResolver() {
return new ExternalUserDetailsArgumentResolver();
}
@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(userDetailsArgumentResolver());
}
}
当我启动应用程序时,调用MVCConfig并调用所有它的@Bean注释方法,但不会调用addArgumentResolvers。
在我的控制器中我有这个
@RequestMapping(value = "/test", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
RequestInfo createRequest(@ExternalUserDetails ExternalUser userDetails , @RequestBody final RequestInfo request) {
//do something here
}
我得到的只是这个错误
java.lang.IllegalArgumentException: argument type mismatch
HandlerMethod details:
Controller [my.base.package.controllers.RequestController]
Method [public my.base.package.RequestInfo my.base.package.controllers.RequestController.createRequest(my.base.package.ExternalUser,my.base.package.RequestInfo)]
Resolved arguments:
[0] [type=my.base.package.AuthorizationImpl] [value=my.base.package.AuthorizationImpl@71724981]
[1] [type=my.base.package.RequestInfo] [value=my.base.package.RequestInfo@637ee57]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:218)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:947)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:878)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:946)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:859)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:822)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
这是AuthorizationImpl类
public class AuthorizationImpl implements Authentication {
private ExternalUser user;
// just a simple pojo with getters and setters
}
这是ExternalUser类
public class ExternalUser implements Principal {
private long id;
private String name;
// just a simple pojo with getters and setters
}