如何从 AnnotationMethodHandlerAdapter 迁移到 RequestMappingHandlerAdapter
How to migrate from AnnotationMethodHandlerAdapter to RequestMappingHandlerAdapter
我在 Wicket 管理 GUI 的应用程序中通过 Spring MVC 构建 REST 服务。基本上,我只需要 DispatcherServlet
和一个带有 @RequestMapping
/@RequestBody
.
的控制器
因为服务服务 JSON,我需要设置 MappingJackson2HttpMessageConverter
。我可以通过 AnnotationMethodHandlerAdapter
做到这一点并且效果很好:
@Configuration
@ComponentScan("cz.swsamuraj.wicketspring")
public class SpringRestConfiguration {
@Bean
public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter() {
HttpMessageConverter<?>[] converters = { new MappingJackson2HttpMessageConverter()};
AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
adapter.setMessageConverters(converters);
return adapter;
}
}
问题是 AnnotationMethodHandlerAdapter
已弃用,建议改用 RequestMappingHandlerAdapter
。
但是如果我使用这个配置:
@Configuration
@ComponentScan("cz.swsamuraj.wicketspring")
public class SpringRestConfiguration {
@Bean
public RequestMappingHandlerAdapter requestHandler() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
adapter.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return adapter;
}
}
我收到异常:
javax.servlet.ServletException: No adapter for handler [cz.swsamuraj.wicketspring.ws.api.QuestionApiController@69f8a79f]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1198)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
所以,我的问题是:如何在 RequestMappingHandlerAdapter
中设置处理程序适配器?
我花了几天时间研究,但没有找到任何有用的配置示例 RequestMappingHandlerAdapter
。所有的建议都只是说将 @EnableWebMvc
放在配置上,但这不是因为 Wicket-Spring 共存的方式。
为了提供完整的上下文,我在 Bitbucket 上创建了一个可构建且可运行的小型项目:sw-samuraj/blog-wicket-spring-rest
我能够使用不同的方法解决我的问题 - 使用 WebApplicationInitializer
,我能够将 @EnableWebMvc
注释放在我的配置 class 上,因此 bean 都没有RequestMappingHandlerAdapter
、AnnotationMethodHandlerAdapter
都不是必需的。 JSON 现在工作正常,开箱即用。
配置
@Configuration
@EnableWebMvc
@ComponentScan("cz.swsamuraj.wicketspring")
public class SpringRestConfiguration {
// some additional beans needed for business logic
}
WebApplicationInitializer
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(SpringRestConfiguration.class);
servletContext.addListener(new ContextLoaderListener(dispatcherContext));
ServletRegistration.Dynamic dispatcher =
servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/rest/*");
}
}
示例项目
完整的工作示例在 Bitbucket 上:blog-wicket-spring-rest
我在 Wicket 管理 GUI 的应用程序中通过 Spring MVC 构建 REST 服务。基本上,我只需要 DispatcherServlet
和一个带有 @RequestMapping
/@RequestBody
.
因为服务服务 JSON,我需要设置 MappingJackson2HttpMessageConverter
。我可以通过 AnnotationMethodHandlerAdapter
做到这一点并且效果很好:
@Configuration
@ComponentScan("cz.swsamuraj.wicketspring")
public class SpringRestConfiguration {
@Bean
public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter() {
HttpMessageConverter<?>[] converters = { new MappingJackson2HttpMessageConverter()};
AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
adapter.setMessageConverters(converters);
return adapter;
}
}
问题是 AnnotationMethodHandlerAdapter
已弃用,建议改用 RequestMappingHandlerAdapter
。
但是如果我使用这个配置:
@Configuration
@ComponentScan("cz.swsamuraj.wicketspring")
public class SpringRestConfiguration {
@Bean
public RequestMappingHandlerAdapter requestHandler() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
adapter.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return adapter;
}
}
我收到异常:
javax.servlet.ServletException: No adapter for handler [cz.swsamuraj.wicketspring.ws.api.QuestionApiController@69f8a79f]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1198)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
所以,我的问题是:如何在 RequestMappingHandlerAdapter
中设置处理程序适配器?
我花了几天时间研究,但没有找到任何有用的配置示例 RequestMappingHandlerAdapter
。所有的建议都只是说将 @EnableWebMvc
放在配置上,但这不是因为 Wicket-Spring 共存的方式。
为了提供完整的上下文,我在 Bitbucket 上创建了一个可构建且可运行的小型项目:sw-samuraj/blog-wicket-spring-rest
我能够使用不同的方法解决我的问题 - 使用 WebApplicationInitializer
,我能够将 @EnableWebMvc
注释放在我的配置 class 上,因此 bean 都没有RequestMappingHandlerAdapter
、AnnotationMethodHandlerAdapter
都不是必需的。 JSON 现在工作正常,开箱即用。
配置
@Configuration
@EnableWebMvc
@ComponentScan("cz.swsamuraj.wicketspring")
public class SpringRestConfiguration {
// some additional beans needed for business logic
}
WebApplicationInitializer
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(SpringRestConfiguration.class);
servletContext.addListener(new ContextLoaderListener(dispatcherContext));
ServletRegistration.Dynamic dispatcher =
servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/rest/*");
}
}
示例项目
完整的工作示例在 Bitbucket 上:blog-wicket-spring-rest