28

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.

Jaffa
  • 501
  • 1
  • 8
  • 16

8 Answers8

31

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.

JBCP
  • 13,109
  • 9
  • 73
  • 111
  • From your referenced doc: http://docs.spring.io/spring-boot/docs/1.2.x/reference/html/common-application-properties.html# DATA RESET (RepositoryRestConfiguration}) spring.data.rest.base-uri= # base URI against which the exporter should calculate its links – acsadam0404 Feb 20 '15 at 11:26
  • @adam0404 - both work, see http://docs.spring.io/spring-boot/docs/1.2.x/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding – JBCP Feb 20 '15 at 18:16
  • 16
    In Spring Boot 1.2.3, baseUri is deprecated. Use basePath (or base-path) instead. –  Apr 11 '15 at 04:09
  • In my case (Spring Boot 2.3.2) the property `spring.data.rest.base-path` was being ignored. Turns out I had a `RepositoryRestMvcConfiguration` in my code base (from before switching from Spring to Spring Boot). After deleting that class, the property worked like expected. – murf Sep 23 '20 at 14:05
16

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();
    }
  }
}
whirlwin
  • 16,044
  • 17
  • 67
  • 98
user3719649
  • 161
  • 1
  • 2
  • This changes the base uri for the links only. – Ethan Anderson Jun 13 '14 at 20:54
  • @EthanAnderson that's right. Wouldn't it (arguably) suffice in the case of a fairly simple application? – user3719649 Jun 16 '14 at 22:09
  • From what I can tell it's no help at all since you'll end up with links pointing to controller endpoints which do not exist. The resource will be accessible off of /resource, but the links to said resource will appear as /data/resource and you will likely get a 404 following a link. – Ethan Anderson Jun 17 '14 at 13:31
  • 2
    On further inspection it looks like the logic changed recently. In my project running SDR 2.0.2.RELEASE it just changes the links, but in 2.1.0.RELEASE it appears to pass in the baseUri() when instantiating the relevant beans. – Ethan Anderson Jun 17 '14 at 17:49
  • 2
    `URI.create("/data")` is helpful to avoid having to catch the checked exception (throws runtime instead) – Matt Whipple Jan 28 '15 at 19:48
  • I do not think you need @Import since you are extending it – Andrea Ratto Aug 07 '15 at 16:02
16

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.

taynguyen
  • 2,961
  • 1
  • 26
  • 26
  • 1
    Your config works for me with Spring Boot 1.5.4. However, I have problems with spring security. So I solve the issue this way (with yml syntax): "spring: data: rest: base-path: api" – Cassio Seffrin Nov 06 '17 at 01:27
8

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

spring.mvc.servlet.path=/rest

Hope this helps

6

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";
    }
}
whirlwin
  • 16,044
  • 17
  • 67
  • 98
Jaffa
  • 501
  • 1
  • 8
  • 16
  • 2
    You can also configure it via web.xml too right ? using `RepositoryRestDispatcherSerlvet` or `RepositoryRestExporterServlet`. is there any need of using Java configuration if u r already using web.xml ? – Aman Gupta May 10 '14 at 12:33
4

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");
      }
    };
  }
}
Krzysztof Szewczyk
  • 1,752
  • 1
  • 21
  • 25
0

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

Community
  • 1
  • 1
venergiac
  • 7,469
  • 2
  • 48
  • 70
0

You set the property, e.g. in your YAML file:

spring.data.rest.base-path=/rest
tomsv
  • 7,207
  • 6
  • 55
  • 88