我有一个带有DispatcherServlet和基于XML的配置的现有Spring MVC应用程序。
现在,我想集成Spring Data REST,但是我不知道如何以一种干净的方式进行此操作。我加了
<context:component-scan>...</context:component-scan>
所以找到了我的RestControllers,但是我没有添加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 *方法。
在Spring Data REST的文档中,有一章介绍了如何在代码中向Spring MVC应用程序添加Spring Data REST。它也说
如果您仍在Servlet 2.5环境中,则标准web.xml中与上述等效的内容也将与此配置完全相同。
你怎么做到这一点?
幸运的是,在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"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句