Can a spring boot @RestController be enabled/disabled using properties?

后端 未结 4 885
别那么骄傲
别那么骄傲 2020-12-01 01:42

Given a \"standard\" spring boot application with a @RestController, eg

@RestController
@RequestMapping(value = \"foo\", produces = \"applicatio         


        
4条回答
  •  粉色の甜心
    2020-12-01 02:20

    I assume this question comes from the fact that you are using different application.properties files for your different enviroments. In this case you can use spring profiles and separate configurations into different files with profile name suffix for example:

    application.properties:

    spring.profiles.active=@activatedProperties@
    

    application-local.properties:

     //some config
    

    application-prod.properties:

    //some config
    

    then in your build paramethers you can specify which enviroment are you building by adding option:

    -Dspring.profiles.active= //<-put here profile local or prod
    

    then in your application you can enable/disable any spring bean by adding

    @Profile("put here profile name")
    

    for example:

    @RestController
    @Profile("local")
    @RequestMapping("/testApi")
    public class RestForTesting{
    
    //do some stuff
    
    }
    

    now my RestForTesting will be created only if im running a build created with

    -Dspring.profiles.active=local

提交回复
热议问题