gpt4 book ai didi

java - 将 Spring 3.1 与 RequestMappingHandlerAdapter 一起使用时出现默认构造函数错误

转载 作者:行者123 更新时间:2023-12-01 04:58:50 25 4
gpt4 key购买 nike

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

建议?

我已经更新了 java 代码以使用 HandlerMethodArgumentResolver(它是 3.0.7 中的 AnnotationMethodHandlerAdapter),但是在进行运行时集成测试时会出现此堆栈跟踪:

 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

SecuritySystemTestStartUp 中的测试驱动程序代码使用 @ContextConfiguration 进行注释:

 @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;
}
}

参数解析器(与 Spring 3.0.7 兼容)用于 postProcessAfterInitialization 中。下面是参数解析器的代码摘录。

public class AnnotationMethodHandlerBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

@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);
}

...

}

并在 Spring 配置中指定为 ...

<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>

参数解析器的流程如下:

@Component public class SecurityRequestParametersArgumentResolver implements HandlerMethodArgumentResolver { @Autowired // implicitly autowired (not in xml) private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

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>

最佳答案

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

public boolean supportsParameter(MethodParameter parameter)
{
return parameter.getParameterAnnotation(ParamResolver.class)!=null
}

关于java - 将 Spring 3.1 与 RequestMappingHandlerAdapter 一起使用时出现默认构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13650641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com