简体   繁体   中英

How to find port number of a Spring Boot app?

How do I find the port number of my SpringBoot app so I can call it? I already tried setting -Dserver.port=8080 and--server.port=8080 in VM arguments and in src/main/resources/application.properties.

If I try to go to localhost:8080 Chrome says refused to connect. I simply want to be able to connect to my App don't know why Spring Boot made finding which port is being used so challenging.

I'm using Eclipse and the app appears to be running properly from the logs.

This simply printed 0:

@Value("${local.server.port}")
int port;

I've tried these answers none work: Spring Boot - How to get the running port Spring boot - how to get running port and ip address How to configure port for a Spring Boot application How to configure port for a Spring Boot application

localhost:<your port> may not respond at all. Make sure you're hitting an endpoint you know is availible like localhost:8080/info .

As @BrianC mentioned make sure your app is actually a web server.

in your springboot main method use below code to detect port of your application

  Environment environment =app.run(args).getEnvironment();
    System.out.println(environment.getProprty("server.port");

this will tell you actual port where your aplication running.

Value can be found using Spring's Environment as it represents the environment in which the current application is running. It can also be used to find other properties in applications like profiles.

 @Autowired Environment environment; 
 String port = environment. getProperty("local.server.port");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM