Подтвердить что ты не робот

Spring Базовый путь базы данных

Я добавил Spring Data Rest (2.0) в существующее приложение Spring MVC, создав класс конфигурации Java, который расширяет RepositoryRestMvcConfiguration и добавляет @RestResource в репозитории.

Можно ли изменить базовый URL для Rest API? Например:

http://localhost:8080/rest/customers

вместо

http://localhost:8080/customers

Я попытался переопределить configureRepositoryRestConfiguration с помощью setBaseURI, но, похоже, он не применим ко всем ссылкам в ответе.

4b9b3361

Ответ 1

Я решил свою проблему, добавив второй "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

По 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.

Ответ 3

Вы можете настроить 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();
    }
  }
}

Ответ 4

Я использовал spring boot 1.2.3.REALEASE Я пробовал spring.data.rest.baseUri=/api и spring.data.rest.basePath=/api, но он не работает.

После попытки и googling: server.servlet-path=/api работал у меня.

Ответ 5

Посмотрите официальную документацию как изменить базовый уровень ура

Но я не знаю, почему для меня свойство spring.data.rest.basePath=/api не работает, и я должен написать второе решение:

@Configuration
class CustomRestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurerAdapter() {

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

Ответ 7

Добавьте следующую строку в application.properties(Spring boot version 2.2.0.M2)

spring.mvc.servlet.path=/rest

Надеюсь это поможет