我在spring-mvc应用程序中使用spring-data-rest(1.0.0.RELEASE) 但是我遇到了以下问题
在我的WebConfig
中@Import(RepositoryRestMvcConfiguration.class)
public static class WebConfiguration extends WebMvcConfigurationSupport{...}
此案例RepositoryRestMvcConfiguration
有一个bean RepositoryRestController
,其方法listRepositories(...)
带注释
@RequestMapping(value = "/", method = RequestMethod.GET)
现在的问题是,当我点击根上下文("/")
时,我会收到指向我的存储库的链接,如下所示
{
"links" : [ {
"rel" : "content",
"href" : "http://localhost:7070/appName/content"
}, {
"rel" : "language",
"href" : "http://localhost:7070/appName/language"
} ],
"content" : [ ]
}
但我想改为显示index.html文件。
我想在根上下文中禁用链接的发现。
事件我尝试使用自定义控制器映射到根上下文("/")
,但第一优先级的弹簧与RepositoryRestController's.listRepositories(...)
方法匹配。
它不会出现在我的控制器方法中。 在日志中,它就像这样
**RepositoryRestHandlerMapping**: 185 - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryRestController.listRepositories(org.springframework.http.server.ServletServerHttpRequest,java.net.URI) throws java.io.IOException
**RequestMappingHandlerMapping**: 185 - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String xxx.xx.xx.AccessController.getIndex()
答案 0 :(得分:0)
我遇到了同样的问题,并解决了以下问题:
1)切换到spring-data-rest-webmvc 1.1.0.M1
2)将您的上下文配置拆分为web-config和rest-config
WebConfig.java
@Configuration
@EnableHypermediaSupport
@EnableSpringDataWebSupport
@EnableWebMvc
@ComponentScan(basePackages = {"com.yourcompanyname.XXX"})
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass( JstlView.class );
viewResolver.setPrefix( "/WEB-INF/views/" );
viewResolver.setSuffix( ".jsp" );
return viewResolver;
}
@Bean
public ReloadableResourceBundleMessageSource messageSource()
{
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename( "/resources/messages" );
messageSource.setCacheSeconds( 0 );
return messageSource;
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping() {
@Override
protected boolean isHandler(Class<?> beanType) {
return beanType == null || ClassUtils.getPackageName(beanType).startsWith("org.springframework.data") ? false
: super.isHandler(beanType);
}
};
handlerMapping.setOrder(0);
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager());
return handlerMapping;
}
@Override
@Bean
public HandlerMapping resourceHandlerMapping() {
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
handlerMapping.setOrder(-1);
return handlerMapping;
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}
RESTConfig.java
@Configuration
public class RESTConfig extends RepositoryRestMvcConfiguration {
}
然后创建webapp初始化程序类,替换web.xml中的注册(需要servlet-api 3.0+)。在下面的示例中,spring-data-rest路径设置为/ rest / ,还包括我自己建议的JPA配置类。我们将在2个不同的类中注册2个servlet,一个用于主“/”,一个用于“/ rest / ”路径:
WebAppInitializer.java
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getRootConfigClasses()
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { JpaConfig.class };
}
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getServletConfigClasses()
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletMappings()
*/
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletFilters()
*/
@Override
protected javax.servlet.Filter[] getServletFilters() {
return new javax.servlet.Filter[] { new OpenEntityManagerInViewFilter() };
}
}
RestAppConfig.java
public class RestAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getRootConfigClasses()
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { JpaConfig.class };
}
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getServletConfigClasses()
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { RESTConfig.class };
}
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletMappings()
*/
@Override
protected String[] getServletMappings() {
return new String[] { "/rest/*" };
}
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletFilters()
*/
@Override
protected javax.servlet.Filter[] getServletFilters() {
return new javax.servlet.Filter[] { new OpenEntityManagerInViewFilter() };
}
}
答案 1 :(得分:0)
以下是我如何实现这个目标
子类RepositoryRestMvcConfiguration,然后抛出一些自定义异常
在Advised Controller中捕获异常并返回“index.html”
使用子类来导入配置
@Configuration
public class CustomRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {
@Override
public RepositoryRestController repositoryRestController() throws Exception {
RepositoryRestController restController = new RepositoryRestController(){
@Override
public ResponseEntity<?> listRepositories(ServletServerHttpRequest request, URI baseUri) throws IOException {
throw new RootContextRepositoryException();
}
};
return restController;
}
}
@ControllerAdvice
public class BaseAdvisedController {
@ExceptionHandler({RootContextRepositoryException.class})
public String rootContextRepositoryExceptionHandler(RootContextRepositoryException ex){
return "index.html";
}
}
@Import(CustomRepositoryRestMvcConfiguration.class)
public static class WebConfiguration extends WebMvcConfigurationSupport{...}
这解决了我的问题