8

I have a spring boot application with bunch of rest controllers (@RestController).

I use the following property in application.properties file to set the base url:

server.context-path=api

This property changes the base url for my static resources, too. I don't want them to change, how can I do that?

Note1 Why I want to do that? I want to serve a single page application (react app) on my server, and I want most of my requests made to /api/** to be authorized. I want all other GET requests to be handled by the react router. This is why I don't want the base URL for my static resources to change.

Arian
  • 7,397
  • 21
  • 89
  • 177

4 Answers4

14

You should not use this property as it changes the context path for the whole application.

Why not simply specify /api/yourResource in the RequestMapping annotation such as :

@RestController
@RequestMapping("/api/oneController")
public class OneController { ... }

.....

@RestController
@RequestMapping("/api/anotherController")
public class AnotherController { ... }
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 3
    I'm looking for a way to do this as programmatically as possible, and control it in a one central point. – Arian Nov 23 '17 at 19:55
11

You can use

spring.data.rest.base-path=/api

in your application properties with

@BasePathAwareController 

on your controller class.

When you use

server.context-path=ctx

the context path applies to the whole application including

  • static resources
  • end points created with @Controller
  • end points created with @RestController
  • end points created with @BasePathAwareContoller
  • end points created with @RepositoryRestController
  • end points created with @RepositoryRestResource

When you use

spring.data.rest.base-path=/api

the prefix applies to

  • end points created with @BasePathAwareContoller
  • end points created with @RepositoryRestController
  • end points created with @RepositoryRestResource

And you can use both

server.context-path=ctx
spring.data.rest.base-path=/api

to apply a prefix like /ctx/api/

vsoni
  • 2,828
  • 9
  • 13
  • I don't think it's that straight. Look at here: https://stackoverflow.com/questions/47428518/spring-data-rest-base-path-makes-two-endpoints-available/47447580?noredirect=1#comment81884619_47447580 – Arian Nov 24 '17 at 06:55
  • My suggestion/idea was to use spring.data.rest.base-path in properties file and @BasePathAwareContoller in controllers (in place of @Controller/@RestController). I use it with spring boot 1.5.8.RELEASE, and it works fine. – vsoni Nov 24 '17 at 07:54
  • that's right, but there will be a shadow endpoint available at the URL without the path, too. – Arian Nov 24 '17 at 16:42
  • I don't see any shadow endpoints in my environment. I am using spring boot 1.5.8.RELEASE. – vsoni Nov 25 '17 at 05:46
  • Thanks. You're my hero. No one suggested to annotate the controller class with BasePathAwareController – Buckstabue May 12 '18 at 02:15
3

You can define your path as a property in the .properties file and read it using the @Value annotation

example

in the application.properties

common.basepath = /test

in the controller use

@RequestMapping(@Value("${common.basepath}"))
Amr Alaa
  • 545
  • 3
  • 7
0

Try this:

@RequestMapping(ControllerConstant.BASE_API_URL + "/audit")

where:

public class ControllerConstant {
    public static final String BASE_API_URL = "/whatever/api/v1";
}

This way the base URL is centralized in ControllerConstant.BASE_API_URL.

I hope this helps.