Java 如何将 Spring Boot @RepositoryRestResource 映射到特定 url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24577400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:08:42  来源:igfitidea点击:

How can I map a Spring Boot @RepositoryRestResource to a specific url?

javaspringconfigurationspring-dataspring-boot

提问by stratosgear

I cannot seem to be able to map my Repository in any location other than the following:

除了以下位置,我似乎无法在任何位置映射我的存储库:

@RepositoryRestResource(collectionResourceRel = "item", path = "item")
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {

I thought I can use:

我以为我可以使用:

 path = "/some/other/path/item"

but the mapping does not resolve. I get:

但映射没有解决。我得到:

HTTP ERROR 404

Problem accessing /some/other/path/item. Reason:

Not Found

In spring-data javadoc pathis defined as: "The path segment under which this resource is to be exported."

在 spring-data javadocpath中定义为:“要导出此资源的路径段。”

What am I doing wrong?

我究竟做错了什么?

回答by Dave Syer

I think the path attribute is used to specify a path segment(so no slashes). The "/some/other/path" would have to be the servlet path or the context path (i.e. nothing to do with Spring Data).

我认为 path 属性用于指定路径(所以没有斜线)。“/some/other/path”必须是 servlet 路径或上下文路径(即与 Spring Data 无关)。

回答by gmich

You need to extend the RepositoryRestMvcConfigurationand override the configureRepositoryRestConfiguration(RepositoryRestConfiguration config)to set yours baseUri. e.g.

您需要扩展RepositoryRestMvcConfiguration并覆盖configureRepositoryRestConfiguration(RepositoryRestConfiguration config)以设置您的baseUri. 例如

@Configuration
public class MyRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {

    private static final String MY_BASE_URI_URI = "/my/base/uri";

    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        super.configureRepositoryRestConfiguration(config);
        config.setBaseUri(URI.create(MY_BASE_URI_URI));
    }
}

回答by Bruce Edge

To change the base URI, you can also just add this to application.properties:

要更改基本 URI,您也可以将其添加到 application.properties:

spring.data.rest.base-path=/my/base/uri

回答by Attila Csányi

Correct application property is the following: spring.data.rest.base-path=/my/base/path(base-pathinstead of base-uri)

正确的应用特性如下: spring.data.rest.base-path=/my/base/pathbase-path代替base-uri

回答by Ronan Quillevere

In spring boot 2

在弹簧靴 2

@Configuration
public class RepositoryConfiguration extends RepositoryRestConfigurerAdapter
{

   @Override
   public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config)
   {
      config.setBasePath("/my/base/uri");
   }
}