如何从AnnotationMethodHandlerAdapter迁移到RequestMappingHandlerAdapter

时间:2017-05-12 20:21:53

标签: spring rest spring-mvc wicket

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

1 个答案:

答案 0 :(得分:0)

我能够用不同的方法解决我的问题 - 使用WebApplicationInitializer,我能够将@EnableWebMvc注释放在我的配置类上,因此既没有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