Home > OS >  argumentResolver cannot be resolved to a variable in Spring 5.3.20 for HandlerMethodArgumentResolver
argumentResolver cannot be resolved to a variable in Spring 5.3.20 for HandlerMethodArgumentResolver

Time:07-10

I am migrating Spring standalone application from Spring 2.3.2.RELEASE to 5.3.20 and below code is breaking

Method method = methodNameToMethodObjectCache.get(methodName);

        MockHttpServletResponse response = new MockHttpServletResponse();
        ServletWebRequest webRequest = new ServletWebRequest(request, response);

        DefaultDataBinderFactory binderFactory = new DefaultDataBinderFactory(requestMappingHandlerAdapter.getWebBindingInitializer());

        HandlerMethodArgumentResolverComposite argumentResolver = requestMappingHandlerAdapter.getArgumentResolvers();
        LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

        HandlerMethod handlerMethod = new HandlerMethod(controllerClass, method);
        InvocableHandlerMethod requestMethod = new InvocableHandlerMethod(handlerMethod);
        requestMethod.setDataBinderFactory(binderFactory);

        requestMethod.setHandlerMethodArgumentResolvers(argumentResolver);
        requestMethod.setDataBinderFactory(binderFactory);
        requestMethod.setParameterNameDiscoverer(nameDiscoverer);

I change code like below : Here I am getting HandlerMethodArgumentResolver class, what I need is HandlerMethodArgumentResolverComposite, how can I do that?

List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();

CodePudding user response:

Just create the HandlerMethodArgumentResolverComposite instance and set its resolvers:

HandlerMethodArgumentResolverComposite argumentResolver = new HandlerMethodArgumentResolverComposite();
argumentResolver.addResolvers(requestMappingHandlerAdapter.getArgumentResolvers());
  • Related