【问题标题】:Spring Data REST with Spring MVC: Adding RepositoryRestMvcConfiguration to existing DispatcherServletSpring Data REST with Spring MVC:将 RepositoryRestMvcConfiguration 添加到现有 DispatcherServlet
【发布时间】:2015-04-14 15:09:36
【问题描述】:

我有一个现有的带有 DispatcherServlet 和基于 XML 的配置的 Spring MVC 应用程序。

现在我想集成 Spring Data REST,但我不知道如何以干净的方式做到这一点。我加了

<context:component-scan>...</context:component-scan>

所以找到了我的 RestController,但我未能添加 RepositoryRestMvcConfiguration 配置。我尝试了不起作用的注释驱动方法

@Configuration
public class RestConfiguration extends RepositoryRestMvcConfiguration {
...
}

<bean class="com.mypackage.rest.RestConfiguration" />

方法也不起作用。

我还尝试了 web.xml 中的以下内容

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.mypackage.rest.RestConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

奇怪的是,一个用@PostConstruct注解的方法被调用了,但不是configure*方法。

docs for Spring Data REST 中有一章解释了如何在代码中将 Spring Data REST 添加到 Spring MVC 应用程序中。它还说

如果您仍处于 servlet 2.5 环境中,则标准 web.xml 中的上述等效项也将与此配置相同。

你是怎么做到的?

【问题讨论】:

    标签: spring spring-mvc web.xml spring-data-rest


    【解决方案1】:

    幸运的是,在Section 11.2 中有解释。如果在第 2.5 节中有一个指向第 11.2 节的参考,那就太好了:-/

    在 Java 中,这看起来像:

    import org.springframework.context.annotation.Import;
    import org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration;
    
    @Configuration
    @Import(RepositoryRestMvConfiguration.class)
    public class MyApplicationConfiguration {
      …
    }
    

    在 XML 中,这看起来像:

    <bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2021-05-07
      • 2010-10-31
      • 2017-09-16
      相关资源
      最近更新 更多