Pergunta

I have added Spring Data Rest (2.0) to an existing Spring MVC application by creating a Java config class that extends RepositoryRestMvcConfiguration, and adding @RestResource to the repositories.

Is it possible to change the base URL for the Rest API? E.g:

http://localhost:8080/rest/customers

instead of

http://localhost:8080/customers

I tried to override configureRepositoryRestConfiguration using setBaseURI, but it didn't seem to apply to all links in the response.

Foi útil?

Solução 4

I solved my problem by adding a second "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";
    }
}

Outras dicas

As of Spring Boot 1.2 you are able to set this property:

spring.data.rest.baseUri=api

Alternatively:

spring.data.rest.base-uri=api

(Spring Boot uses a relaxed binding system)

NOTE: I have found that if you have extended RepositoryRestMvcConfiguration with custom configuration, the property does not take effect. For more information see:

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

Once the next version of Spring Boot is released (after 1.2.1), the solution will be to extend RepositoryRestMvcBootConfiguration instead.

I used spring boot 1.2.3.REALEASE I tried spring.data.rest.baseUri=/api and spring.data.rest.basePath=/api but it not working.

After try and googling: server.servlet-path=/api worked for me.

You can configure the RepositoryRestMvcConfiguration by overriding it in the following manner:

@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();
    }
  }
}

Add to following line to application.properties(Spring boot version 2.2.0.M2)

spring.mvc.servlet.path=/rest

Hope this helps

See official documentation

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

server.servlet-path=/ # Path of the main dispatcher servlet.
server.context-path=

you can include it on the configuration file.

See also Add context path to Spring Boot application

Look at official documentation how to change rest base uri

But I don't know why for me spring.data.rest.basePath=/api property is not working and I must wrote second solution:

@Configuration
class CustomRestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurerAdapter() {

      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setBasePath("/api");
      }
    };
  }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top