Spring Data Rest基本路径

Jaf*_*ffa 22 spring-data-rest

我已经通过创建扩展RepositoryRestMvcConfiguration的Java配置类并将@RestResource添加到存储库,将Spring Data Rest(2.0)添加到现有的Spring MVC应用程序中.

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

http://localhost:8080/rest/customers
Run Code Online (Sandbox Code Playgroud)

代替

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

我尝试使用setBaseURI覆盖configureRepositoryRestConfiguration,但它似乎不适用于响应中的所有链接.

JBC*_*BCP 27

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.

  • 在Spring Boot 1.2.3中,不推荐使用baseUri.请改用basePath(或base-path). (13认同)

小智 15

您可以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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 经过进一步检查,似乎逻辑最近发生了变化。在我运行 SDR 2.0.2.RELEASE 的项目中,它只是更改了链接,但在 2.1.0.RELEASE 中,它似乎在实例化相关 bean 时传入了 baseUri()。 (2认同)
  • `URI.create("/ data")`有助于避免必须捕获已检查的异常(抛出运行时) (2认同)

tay*_*yen 15

我用弹簧启动1.2.3.REALEASE我试过spring.data.rest.baseUri=/apispring.data.rest.basePath=/api,但它不能正常工作.

尝试和谷歌搜索后:server.servlet-path=/api为我工作.


Jaf*_*ffa 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您也可以通过web.xml配置它吗?使用`RepositoryRestDispatcherSerlvet`或`RepositoryRestExporterServlet`.如果您已经使用web.xml,是否需要使用Java配置? (2认同)

Mam*_*deh 5

将以下行添加到 application.properties(Spring boot version 2.2.0.M2)

spring.mvc.servlet.path=/rest
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助