当前位置: 首页 > 知识库问答 >
问题:

使用Spring 3.1和RequestMappingHandlerAdapter时出现默认构造函数错误

郦何平
2023-03-14

我有一个自定义的参数解析器SecurityRequestParametersarGumentResolver,它在Spring 3.0.7中工作,但在Spring 3.1.2中失败。下面显示堆栈跟踪和测试驱动程序代码。
当我查看堆栈跟踪时,似乎没有调用SecurityRequestParametersarGumentResolver.Resolve()。相反,我在堆栈跟踪中看到HandlerMethodArgumentResolverComposite.Resolve

建议?

 Caused by: java.lang.NoSuchMethodException: xyz.security.web.SecurityRequestParameters.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
    at org.springframework.beans.BeanUtils.BeanUtils.BeanUtils.instantiateClass(Class<T>) line: 104     
                 ServletModelAttributeMethodProcessor(ModelAttributeMethodProcessor).createAttribute(String, MethodParameter, WebDataBinderFactory, NativeWebRequest) line: 132          
                 ServletModelAttributeMethodProcessor.createAttribute(String, MethodParameter, WebDataBinderFactory, NativeWebRequest) line: 81       
                ServletModelAttributeMethodProcessor(ModelAttributeMethodProcessor).resolveArgument(MethodParameter, ModelAndViewContainer, NativeWebRequest, WebDataBinderFactory) line: 103             
                HandlerMethodArgumentResolverComposite.resolveArgument(MethodParameter, ModelAndViewContainer, NativeWebRequest, WebDataBinderFactory) line: 75     
                ServletInvocableHandlerMethod(InvocableHandlerMethod).getMethodArgumentValues(NativeWebRequest, ModelAndViewContainer, Object...) line: 156     
                ServletInvocableHandlerMethod(InvocableHandlerMethod).invokeForRequest(NativeWebRequest, ModelAndViewContainer, Object...) line: 117     
                ServletInvocableHandlerMethod.invokeAndHandle(NativeWebRequest, ModelAndViewContainer, Object...) line: 96           
                RequestMappingHandlerAdapter.invokeHandlerMethod(HttpServletRequest, HttpServletResponse, HandlerMethod) line: 617            
                RequestMappingHandlerAdapter.handleInternal(HttpServletRequest, HttpServletResponse, HandlerMethod) line: 578         
                RequestMappingHandlerAdapter(AbstractHandlerMethodAdapter).handle(HttpServletRequest, HttpServletResponse, Object) line: 80    
                SecuritySystemTestStartUp$1(DispatcherServlet).doDispatch(HttpServletRequest, HttpServletResponse) line: 923                
                SecuritySystemTestStartUp$1(DispatcherServlet).doService(HttpServletRequest, HttpServletResponse) line: 852                
                SecuritySystemTestStartUp$1(FrameworkServlet).processRequest(HttpServletRequest, HttpServletResponse) line: 882               
                SecuritySystemTestStartUp$1(FrameworkServlet).doPost(HttpServletRequest, HttpServletResponse) line: 789                
                SecuritySystemTestStartUp$1(HttpServlet).service(HttpServletRequest, HttpServletResponse) line: 637              
                SecuritySystemTestStartUp$1(HttpServlet).service(ServletRequest, ServletResponse) line: 717                
                SecurityExternalAPISystemTest.testEncrypt() line: 86     
                 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]        
                 NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available               
                 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available       
                 Method.invoke(Object, Object...) line: not available       
                 FrameworkMethod$1.runReflectiveCall() line: 44             
                 FrameworkMethod$1(ReflectiveCallable).run() line: 15 
                 FrameworkMethod.invokeExplosively(Object, Object...) line: 41              
                 InvokeMethod.evaluate() line: 20            
                 RunBeforeTestMethodCallbacks.evaluate() line: 74         
                 RunAfterTestMethodCallbacks.evaluate() line: 83            
                 SpringRepeat.evaluate() line: 72               
                 SecuritySpringJUnitRunner(SpringJUnit4ClassRunner).runChild(FrameworkMethod, RunNotifier) line: 231           
                 SecuritySpringJUnitRunner(BlockJUnit4ClassRunner).runChild(Object, RunNotifier) line: 49         
                 ParentRunner$3.run() line: 193 
                 ParentRunner$1.schedule(Runnable) line: 52     
                 SecuritySpringJUnitRunner(ParentRunner<T>).runChildren(RunNotifier) line: 191            
                 ParentRunner<T>.access$000(ParentRunner, RunNotifier) line: 42           
                 ParentRunner$2.evaluate() line: 184       
                 RunBefores.evaluate() line: 28  
                 RunBeforeTestClassCallbacks.evaluate() line: 61               
                 RunAfterTestClassCallbacks.evaluate() line: 71   
                 SecuritySpringJUnitRunner(ParentRunner<T>).run(RunNotifier) line: 236             
                 SecuritySpringJUnitRunner(SpringJUnit4ClassRunner).run(RunNotifier) line: 174               
                 JUnit4TestClassReference(JUnit4TestReference).run(TestExecution) line: 50      
                 TestExecution.run(ITestReference[]) line: 38      
                 RemoteTestRunner.runTests(String[], String, TestExecution) line: 467    
                 RemoteTestRunner.runTests(TestExecution) line: 683   
                 RemoteTestRunner.run() line: 390           
                 RemoteTestRunner.main(String[]) line: 197
 @RunWith(SecuritySpringJUnitRunner.class)
 @ContextConfiguration(loader = MockServletContextWebContextLoader.class,
         locations = { "classpath:/spring-system-test.xml" })
 public abstract class SecuritySystemTestStartUp
 {
     private static DispatcherServlet dispatcherServlet;

     ...     
     public static DispatcherServlet getDispatcherServlet()
     {
         try
         {
             if (dispatcherServlet == null)
             {
                 final GenericWebApplicationContext context = new GenericWebApplicationContext();

                 context.setParent(MockServletContextWebContextLoader.getInstance());
                 context.refresh();

                 dispatcherServlet = new DispatcherServlet(context);
                 dispatcherServlet.init(new MockServletConfig());
             }
         }
         catch (final Exception ex)
         ...
         return dispatcherServlet;
     }
 }
@Autowired
public AnnotationMethodHandlerBeanPostProcessor(final SecurityRequestParametersArgumentResolver resolver)
{
    super();
    _resolver = resolver;
}

@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName)
{
     if (bean instanceof RequestMappingHandlerAdapter)
    {
        final RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
        final List<HttpMessageConverter<?>> converters =
            adapter.getMessageConverters();

        converters.add(new BinaryMessageConverter());
        List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
        customArgumentResolvers.add(_resolver);

        adapter.setCustomArgumentResolvers(customArgumentResolvers);
        adapter.setMessageConverters(adapter.getMessageConverters());
    }

    return super.postProcessAfterInitialization(bean, beanName);
}

...
<bean id="SecurityRequestParametersArgumentResolver"
    class="xyz.security.web.SecurityRequestParametersArgumentResolver">
    <constructor-arg ref="CredentialsManager" />
    <constructor-arg ref="TokenService" />
</bean>

<!-- A bean post-processor that registers the SecurityRequestParametersArgumentResolver. -->
<bean class="xyz.security.web.AnnotationMethodHandlerBeanPostProcessor">
    <constructor-arg index="0" ref="SecurityRequestParametersArgumentResolver" />
</bean>
private final CredentialsManager _credentialsManager;
private final TokenService _tokenService;

private RequestResponseBodyMethodProcessor requestResponseBodyMethodProcessor = null;

/* For the component scan... */
SecurityRequestParametersArgumentResolver()
{
    this(null, null);
}

public SecurityRequestParametersArgumentResolver(final CredentialsManager credentialsManager,
        final TokenService tokenService)
{
    super();
    _credentialsManager = credentialsManager;
    _tokenService = tokenService;
}

public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception
{

    if (parameter != null && SecurityRequestParameters.class.equals(parameter.getParameterType()))
    {
        String productName = webRequest.getHeader(HttpHeader.PRODUCT_CONTAINER_HEADER.value());

        ...

        return new SecurityRequestParameters(credentials, contentType, acceptType, dataEncoding,
                sessionIdentifier, remoteIpAddress, securityProductName);
    }
    return new Object();
}

public boolean supportsParameter(MethodParameter parameter)
{
    return getRequestResponseBodyMethodProcessor().supportsParameter(parameter);
}

private RequestResponseBodyMethodProcessor getRequestResponseBodyMethodProcessor()
{
    if (requestResponseBodyMethodProcessor == null)
    {
        List<HttpMessageConverter<?>> messageConverters =
                requestMappingHandlerAdapter.getMessageConverters();
        requestResponseBodyMethodProcessor = new RequestResponseBodyMethodProcessor(messageConverters);
    }
    return requestResponseBodyMethodProcessor;
}

最后...这里是Spring配置中的注释驱动标记

  <mvc:annotation-driven>
    <mvc:argument-resolvers>
      <bean class="com.trgr.cobalt.security.web.SecurityRequestParametersArgumentResolver" />
    </mvc:argument-resolvers>
 </mvc:annotation-driven>

共有1个答案

訾高明
2023-03-14

鉴于您的评论,我认为您必须将supportsparameter更改为以下内容:

public boolean supportsParameter(MethodParameter parameter)
{
    return parameter.getParameterAnnotation(ParamResolver.class)!=null
}
 类似资料:
  • 问题内容: 不知何故,在下面的Node类中,and变量在Node的所有实例之间共享。 有什么办法可以让我继续使用构造函数参数的默认值(在这种情况下为空列表),但要同时获取它们和拥有其自身的变量以及变量? 我正在使用python 3.1.2。 问题答案: 可变的默认参数通常不会执行您想要的操作。相反,请尝试以下操作:

  • 问题内容: 默认构造函数到底是什么?你能告诉我以下哪个是默认构造函数,它与其他构造函数有何不同? 问题答案: 他们都不是。如果定义,则不是默认值。 除非你定义另一个构造函数,否则默认构造函数是自动生成的无参数构造函数。任何未初始化的字段都将设置为其默认值。对于你的榜样,它看起来像这样假设的类型String,int以及int,那类本身是公共的: 这与 完全没有构造函数。但是,如果定义至少一个构造函数

  • 为什么: 如果一个类不提供任何然后在编译时由编译器给出,但是如果一个类包含,那么默认构造函数不是由编译器提供。 我正在编译下面的代码。它给出了编译错误。 代码: 控制台错误: 当我只调用。工作正常。但是当您想使用参数化构造函数调用

  • 问题内容: 所有, 我正在尝试在一些古老的Java代码中进行一些单元测试(无接口,无抽象等)。 这是一个使用ServletContext的servlet(我假设它是由Tomcat设置的),并且它的数据库信息在web.xml / context.xml文件中设置。现在,我已经弄清楚了如何制作Fake ServletContext,但是代码已经 遍布整个地方(因此替换它是不可行的)。我需要找到一种方法

  • 问题内容: 我正在将Lombok项目与Spring Data JPA一起使用。有什么方法可以将Lombok 与JPA默认构造函数连接? 码: 据我所知,JPA需要默认的构造函数,该构造函数被注解覆盖。有什么解决方法吗? 这段代码给我错误: 问题答案: 更新 根据反馈和John的回答,我已更新了不再使用or的答案,而是通过和创建访问器和变量,通过创建默认构造函数via,最后我们创建了构建器通过via

  • 我想做一个简单的POJO(POKO?)在Kotlin中使用一个默认的空构造函数和一个带有参数的辅助构造函数创建一个类,该类提供属性 这并没有给我和属性: 这为我提供了属性,但它们在实例化后没有设置: 这给了我一个编译错误,说“不允许在二级构造函数参数上使用'var'”: 那么,这是怎么做到的?我如何拥有一个默认构造函数和一个带有参数和属性的二级构造函数?