Spring Data Rest 基础路径

28

我通过创建一个扩展了RepositoryRestMvcConfiguration的Java配置类,并将@RestResource添加到仓库中,向现有的Spring MVC应用程序中添加了Spring Data Rest(2.0)。

是否可以更改Rest API的基本URL?例如:

http://localhost:8080/rest/customers

代替

http://localhost:8080/customers

我尝试使用setBaseURI来覆盖configureRepositoryRestConfiguration,但似乎并未应用于响应中的所有链接。

8个回答

31

Spring Boot 1.2版本开始,您可以设置此属性:

spring.data.rest.baseUri=api

或者:

spring.data.rest.base-uri=api

(Spring Boot使用松散绑定系统)
注意:如果您已使用自定义配置扩展了RepositoryRestMvcConfiguration,则该属性不起作用。有关更多信息,请参见:

https://github.com/spring-projects/spring-boot/issues/2392

一旦Spring Boot的下一个版本发布(在1.2.1之后),解决方案将是扩展RepositoryRestMvcBootConfiguration

从您引用的文档中:http://docs.spring.io/spring-boot/docs/1.2.x/reference/html/common-application-properties.html# DATA RESET(RepositoryRestConfiguration)spring.data.rest.base-uri =#导出程序应根据其链接计算的基本URI - acsadam0404
@adam0404 - 两种方式都可以,参见http://docs.spring.io/spring-boot/docs/1.2.x/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding - JBCP
16
在Spring Boot 1.2.3中,baseUri已被弃用。请改用basePath(或base-path)。 - user41871
在我的情况下(Spring Boot 2.3.2),属性 spring.data.rest.base-path 被忽略了。原来我在代码库中有一个 RepositoryRestMvcConfiguration 类(在从 Spring 切换到 Spring Boot 之前就存在)。删除该类后,该属性按预期工作。 - murf

16
您可以通过以下方式覆盖RepositoryRestMvcConfiguration来进行配置:
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class RestDataConfig  extends RepositoryRestMvcConfiguration {

  @Override
  protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    super.configureRepositoryRestConfiguration(config);
    try {
      config.setBaseUri(new URI("/data"));
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
  }
}

这仅更改链接的基本URI。 - Ethan Anderson
@EthanAnderson 没错。在一个相对简单的应用程序中,这(可以说)足够了吧? - user3719649
据我所知,这没有任何帮助,因为你最终会得到指向不存在的控制器端点的链接。该资源将可通过/resource访问,但对该资源的链接将显示为/data/resource,您可能会在跟随链接时遇到404错误。 - Ethan Anderson
2
进一步检查后发现逻辑最近有所改变。在我的项目中运行SDR 2.0.2.RELEASE时,它只是更改链接,但在2.1.0.RELEASE中,似乎在实例化相关bean时传递了baseUri()。 - Ethan Anderson
2
URI.create("/data") 很有帮助,可以避免不得不捕获已检查异常(而是抛出运行时异常)。 - Matt Whipple
我认为你不需要使用@Import,因为你正在扩展它。 - Andrea Ratto

16

我使用的是Spring Boot 1.2.3.RELEASE版本。 我尝试过spring.data.rest.baseUri=/apispring.data.rest.basePath=/api,但都不起作用。

在尝试并搜索之后:server.servlet-path=/api 对我有效。


1
你的配置对于我在Spring Boot 1.5.4中有效。然而,我在使用Spring Security时遇到了问题。所以我用yml语法解决了这个问题: "spring: data: rest: base-path: api" - Cassio Seffrin

8
在application.properties中添加以下行(适用于Spring Boot版本2.2.0.M2):
spring.mvc.servlet.path=/rest

希望这可以帮助到您。

6
我通过添加第二个“AbstractAnnotationConfigDispatcherServletInitializer”解决了我的问题:
public class RestWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { RepositoryRestMvcConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/rest/*" };
    }

    @Override
    protected Filter[] getServletFilters() {
        return null;
    }

    @Override
    protected String getServletName() {
        return "rest-exporter";
    }
}

2
你也可以通过 RepositoryRestDispatcherSerlvetRepositoryRestExporterServlet 在 web.xml 中进行配置,对吧?如果已经在使用 web.xml,那么是否还需要使用 Java 配置呢? - Aman Gupta

4
请查看官方文档 如何更改rest服务的基础uri

但是我不知道为什么对于我来说,spring.data.rest.basePath=/api 属性不起作用,我必须使用第二个解决方案:

@Configuration
class CustomRestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurerAdapter() {

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

2
你需要使用 spring.data.rest.base-path 而不是 spring.data.rest.basePath - boden

0

0
你可以在 YAML 文件中设置属性,例如:
spring.data.rest.base-path=/rest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接