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

JPA Repository and Spring-data-rest baseURi is /profile

刚刚学习 spring引导(并且刚接触 java:来自 .NET 世界)

PS 关于 spring-data 和 spring-data-rest 的课程。一切顺利

建立了到 MS SQlServer 的测试项目连接。为 FindAll

创建了一些 JPA Repos 和单元测试通过

我没有在应用程序属性中设置 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"
        }
    }
}

第一个问题是,/profile 是从哪里来的?

它不是 基本路径 (url)。这是一个 normal work of SDR:

A profile link, as defined in RFC 6906, is a place to include application-level details. The ALPS draft spec is meant to define a particular profile format, which we explore later in this section.

If you navigate into the profile link at localhost:8080/profile, you see content resembling the following:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/profile"
    },
    "persons" : {
      "href" : "http://localhost:8080/profile/persons"
    },
    "addresses" : {
      "href" : "http://localhost:8080/profile/addresses"
    }
  }
}

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

  • http://localhost:8080/users
  • http://localhost:8080/tasks

顺便说一下,您可以在 three ways 中设置 'base path':

  1. 在'application.properties`

    spring.data.rest.basePath=/api

  2. 注册 bean

    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {
    
      return new RepositoryRestConfigurerAdapter() {
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
          config.setBasePath("/api");
        }
      };
    }
    
  3. 使用 RepositoryRestConfigurer

    的自定义实现
    @Component
    public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {
    
      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setBasePath("/api");
      }
    }