JPA存储库和Spring-data-rest baseURi是/ profile

mar*_*234 4 spring-data-jpa spring-data-rest

刚刚学习springboot(并且从.NET世界开始向Java崭新)

弹簧数据和弹簧数据架过PS课程。一切顺利

与MS SQlServer建立了测试项目连接。创建了一些JPA Repos,并且通过了FindAll通过了单元测试

我在应用程序属性中没有设置base-uri,在浏览其余界面(使用Postman)时,所有内容都显示在/ profile下。

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/profile"
        },
        "users": {
            "href": "http://localhost:8080/profile/users"
        },
        "tasks": {
            "href": "http://localhost:8080/profile/tasks"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个问题是/ profile来自哪里?

Cep*_*pr0 5

它不是基本路径(URL)。这是SDR正常工作

如果导航到位于localhost:8080 / profile的配置文件链接,则会看到以下内容:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/profile"
    },
    "persons" : {
      "href" : "http://localhost:8080/profile/persons"
    },
    "addresses" : {
      "href" : "http://localhost:8080/profile/addresses"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要使用您的实体,您必须使用thees链接:

http://localhost:8080/users
http://localhost:8080/tasks
Run Code Online (Sandbox Code Playgroud)

顺便说一下,您可以通过三种方式设置“基本路径” :

  1. 在“ application.properties”中

spring.data.rest.basePath=/api

  1. 注册一个bean
http://localhost:8080/users
http://localhost:8080/tasks
Run Code Online (Sandbox Code Playgroud)
  1. 通过自定义实现 RepositoryRestConfigurer
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {

  return new RepositoryRestConfigurerAdapter() {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
      config.setBasePath("/api");
    }
  };
}
Run Code Online (Sandbox Code Playgroud)