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

后端 未结 9 1244
野性不改
野性不改 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:07

    I used to declare the configuration in application.properties like this (you can use you own property file)

    server.host = localhost
    server.port = 8081
    

    and in application you can get it easily by @Value("${server.host}") and @Value("${server.port}") as field level annotation.

    or if in your case it is dynamic than you can get from system properties

    Here is the example

    @Value("#{systemproperties['server.host']}")
    @Value("#{systemproperties['server.port']}")

    For a better understanding of this annotation , see this example Multiple uses of @Value annotation

提交回复
热议问题