How to get local server host and port in Spring Boot?

后端 未结 9 1200
野性不改
野性不改 2020-12-09 02:27

I\'m starting up a Spring Boot application with mvn spring-boot:run.

One of my @Controllers needs information about the host and port the a

9条回答
  •  既然无缘
    2020-12-09 03:11

    To get the port number in your code you can use the following:

    @Autowired
    Environment environment;
    
    @GetMapping("/test")
    String testConnection(){
        return "Your server is up and running at port: "+environment.getProperty("local.server.port");      
    }
    

    To understand the Environment property you can go through this Spring boot Environment

提交回复
热议问题