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
How can I map a Spring Boot @RepositoryRestResource to a specific url?
提问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 path
is 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 RepositoryRestMvcConfiguration
and 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-path
instead of base-uri
)
正确的应用特性如下:
spring.data.rest.base-path=/my/base/path
(base-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");
}
}