0

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

Philip Rego
  • 552
  • 4
  • 20
  • 35
  • 1
    Do you have SpringBoot web as a dependancy? If not there will be no web server to connect to. – BrianC Dec 10 '21 at 16:39
  • @BrianC It's a Zuul gateway app. I just tried localhost:8080/info and it worked. localhost:8080 gives the same result as localhost:8081 so I didn't think it was working. – Philip Rego Dec 10 '21 at 16:45
  • Ok, so it is running and you can connect to it. If you run sudo killall java does it still respond on other ports? – BrianC Dec 10 '21 at 17:18
  • @BrianC I have no issue now. SpringBoot wasnt running on all ports. Spring boot wasnt running on localhost:8080 it was only running on localhost:8080/info. localhost:8080 gives error "Connection refused" the same error any unmapped port would give localhost:* – Philip Rego Dec 10 '21 at 17:28

3 Answers3

1

localhost:<your port> may not respond at all. Make sure you're hitting an endpoint you know is availible like localhost:8080/info. Make sure your app has completly started, it could take serveral minutes.

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

Philip Rego
  • 552
  • 4
  • 20
  • 35
0

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 .

Dhiraj Surve
  • 310
  • 2
  • 5
-1

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");