SpringDataRest 并显示欢迎文件列表/禁用链接发现 [英] SpringDataRest and show welcome-file-list/disable link discovery

查看:37
本文介绍了SpringDataRest 并显示欢迎文件列表/禁用链接发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 spring-mvc 应用程序中使用 spring-data-rest(1.0.0.RELEASE)但我遇到了以下问题

I am using spring-data-rest(1.0.0.RELEASE) in my spring-mvc application But I am getting following problem

在我的网络配置中

@Import(RepositoryRestMvcConfiguration.class)
public static class WebConfiguration extends WebMvcConfigurationSupport{...}

这里的例子 RepositoryRestMvcConfiguration 有一个 bean RepositoryRestController,它有方法 listRepositories(...)

Here this case RepositoryRestMvcConfiguration has a bean RepositoryRestController which has method listRepositories(...) annotated with

@RequestMapping(value = "/", method = RequestMethod.GET)

现在的问题是,当我点击根上下文 ("/") 然后我得到了指向我的存储库的链接,如下所示

Now the problem is that when I am hitting at the root context ("/") then I am getting links to my repositories, like below

{
  "links" : [ {
    "rel" : "content",
    "href" : "http://localhost:7070/appName/content"
  }, {
    "rel" : "language",
    "href" : "http://localhost:7070/appName/language"
  } ],
  "content" : [ ]

}

但我想显示 index.html 文件.

But I want to show index.html file instead.

我想禁用在根上下文中发现链接.

I want to disable discovery of links at root context.

事件我尝试用我的自定义控制器映射到根上下文("/") 但 spring 的第一优先级匹配 RepositoryRestController's.listRepositories(...) 方法.

Event I tried with my custom controller to map to the root context("/") but spring at the first priority matches the RepositoryRestController's.listRepositories(...) method.

它不会出现在我的控制器方法中.在日志中它是这样的

Its not coming to my controller method. In log its comes like this

**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()

推荐答案

这是我如何实现的

  1. 子类化RepositoryRestMvcConfiguration,然后抛出一些自定义异常

  1. sub classed RepositoryRestMvcConfiguration, then throwing some custom exception

在 Advised Controller 中捕获异常并返回index.html"

catching the exception in Advised Controller and there returning "index.html"

使用子类在配置中导入

步骤 1

@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;
    }
}

步骤 2

@ControllerAdvice
public class BaseAdvisedController {

    @ExceptionHandler({RootContextRepositoryException.class})
    public String rootContextRepositoryExceptionHandler(RootContextRepositoryException ex){
        return "index.html";
    }

}

步骤 3

@Import(CustomRepositoryRestMvcConfiguration.class)
public static class WebConfiguration extends WebMvcConfigurationSupport{...}

这解决了我的问题

这篇关于SpringDataRest 并显示欢迎文件列表/禁用链接发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆