Search code examples
javaspring-bootgradleapplication.properties

Spring Boot read value from application.properties


In my application.properties I have defined port and server context.

server.port=8080
server.context-path=/SekcjaN

How to read value context to insert it into RequestMapping value in controller.

@RestController
@RequestMapping(value = server.context-path+"/auth")
public class AuthController extends BaseController {
}

Solution

  • You don't need to add server.context-path into your controller RequestMapping as it is already handled by spring boot as base path. So if you are using server.context-path as something all your mapping will be prefixed with server.context-path value.

    You can just use your code like this

    server.port=8080
    server.context-path=/SekcjaN
    

    Controller file looks like

    @RestController
    @RequestMapping(value = "/auth")
    public class AuthController extends BaseController {
    }
    

    Your code will perfectly be called by using <domain>:<port>/SekcjaN/auth/..