Is it also possible to access the management port in a similar way, e.g.:

  @SpringBootTest(classes = {Application.class}, webEnvironment = WebEnvironment.RANDOM_PORT)
  public class MyTest {

    @LocalServerPort
    int randomServerPort;

    @LocalManagementPort
    int randomManagementPort;
Answer from LaughingLemon on Stack Overflow
🌐
Baeldung
baeldung.com › home › spring › spring boot › get the running port in spring boot
Get the Running Port in Spring Boot | Baeldung
December 3, 2025 - For example, in the application.properties file, we can set 7777 as the port our application is running on: ... Alternatively, instead of defining a fixed port, we can let the Spring Boot application run on a random port by setting “0” as the value of the “server.port” property: ... Next, let’s go through the two scenarios and discuss different ways to get the port programmatically at runtime.
🌐
Spring
docs.spring.io › spring-boot › docs › 2.0.5.RELEASE › reference › html › howto-embedded-web-servers.html
75. Embedded Web Servers
For more details, see “Section ... clashes) use server.port=0. You can access the port the server is running on from log output or from the ServletWebServerApplicationContext through its WebServer....
🌐
Cedric-dumont
cedric-dumont.com › 2018 › 05 › 03 › how-to-get-the-running-port-in-springboot-tests
How to get the running port in springboot Tests
decorate your test class with : @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) add a property in your Test class : @LocalServerPort private String serverPort; ... @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class RandomPortTest{ @LocalServerPort private int serverPort; @Test public void validateRandomPort(){ assertThat(serverPort > 0).isTrue(); } }
🌐
YouTube
youtube.com › codewis technologies by nuhman paramban
spring boot running server information server port and server name - YouTube
Finding Server Info in Spring Boot server port and nameSpring Boot offers several ways to access the server's name and port within your application code. The...
Published   May 15, 2024
Views   36
🌐
Apps Developer Blog
appsdeveloperblog.com › home › spring framework › spring boot › get the random port number in spring boot
Get the Random Port Number in Spring Boot - Apps Developer Blog
October 25, 2022 - To get the port number on which your Spring Boot application is running, you can use the @LocalServerPort annotation.
🌐
TutorialsPoint
tutorialspoint.com › spring_boot › spring_boot_tomcat_port_number.htm
Spring Boot - Tomcat Port Number
Spring Boot lets you to run the same application more than once on a different port number. In this chapter, you will learn about this in detail. Note that the default port number 8080. In the application.properties file, we can set custom port number for the property server.port ... Note − If the server.port number is 0 while starting the Spring Boot application, Tomcat uses the random port number.
🌐
CopyProgramming
copyprogramming.com › howto › spring-boot-how-to-get-the-running-port
Spring: Getting the Running Port in Spring Boot: A Guide
March 29, 2023 - In a Spring Boot application, the server port is commonly configured in a properties file or YAML file. This file allows us to specify a fixed or random port for the server. The various methods to retrieve the fixed and random port during runtime have been explored in this article.
🌐
Brainly
brainly.com › computers and technology › high school › how do you check if a spring boot application is running?
[FREE] How do you check if a Spring Boot application is running? - brainly.com
November 27, 2023 - To check if a Spring Boot application is running, you can inspect the console log, utilize the Actuator health endpoint, check the process list of your operating system, or verify that the application's configured port is in use.
Find elsewhere
🌐
Mkyong
mkyong.com › home › spring boot › spring boot test if web server starts on a specific port
Spring Boot Test if web server starts on a specific port - Mkyong.com
August 16, 2023 - package com.mkyong; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class WebServerTests { @LocalServerPort private int port; @Test public void testServerPort() { assertEquals(8181, port, "Server is not running on port 8181"); } } The @LocalServerPort annotation injects the actual web server port. $ git clone https://github.com/mkyong/spring-boot.git ... Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.
🌐
Spring
docs.spring.io › spring-boot › docs › 1.3.0.RELEASE › reference › html › boot-features-testing.html
40. Testing - Spring
See Section 70.4, “Discover the HTTP port at runtime” for a description of how you can discover the actual port that was allocated for the duration of the tests. If you wish to use Spock to test a Spring Boot application you should add a dependency on Spock’s spock-spring module to your ...
🌐
Codemia
codemia.io › knowledge-hub › path › spring_boot_-_how_to_get_the_running_port
Spring Boot - How to get the running port
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
Top answer
1 of 2
1

See how to set the server port, then connect from your client to the port you set on your server.

You have to either:

  1. Follow a convention to know the port you are connecting to.

    • The convention followed may be either the standard port assignment convention, or a custom assignment convention that you establish.

    OR

  2. Publish the port (also usually an ip or dns) to a separate lookup service. Then use the lookup service to locate the target service location.

I recommend you just follow convention unless you have large scale complicated interconnected microservices (which you don’t).

The convention would be:

  • public http (including rest services or soap services) unecrypted on port 80 and encrypted on 443.
  • Internal http services use 8080 unencrypted and 8443 encrypted.

For an example of dynamic service discovery, see:

  • hasicorp consul.

In particular the “Introduction to HashiCorp Consul” video section from minutes two to eight on service discovery. Systems like Kubernetes (adopted by Google Cloud), adopt a similar architecture.

There are other Java based network service discovery systems such as the (defunct) jini.

But, using rest services on the common ports is the standard way that common rest and web services are generally exposed.

One reason for that is that public access to those ports, 80 and 443, are usually allowed by public facing firewalls, but access to most other ports is disallowed.

The three most difficult things in computing are naming things and off by one errors. Finding things is the fourth.

2 of 2
0

For your question, you can reference this question: spring-boot-how-to-get-the-running-port, then print server port when startup.

And I think set a fixed port could be beeter like server.port=8080 in application.properties

🌐
EDUCBA
educba.com › home › software development › software development tutorials › spring tutorial › spring boot port
Spring Boot Port | Complete Guide to Spring Boot Port
March 28, 2023 - The boot tomcat server is run on the default port as 8080. The below example shows that by default spring boot will use port no 8080 to run the application.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Howtostartprogramming
howtostartprogramming.in › home › how to check which port spring boot is running
how to check which port spring boot is running - How to start programming
By default, Spring boot applications start with embedded tomcat server start on default port 8080. We can change default server port to any other port, using any one of below technique.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-run-spring-boot-application
How to Run Spring Boot Application? - GeeksforGeeks
March 2, 2026 - ... Open DemoApplication.java. Right-click ->Run As ->Spring Boot App. After successfully running the application, you can see the console where the Tomcat server starts on default port number 8080 as shown in the below image.
🌐
Baeldung
baeldung.com › home › spring › spring boot › how to change the default port in spring boot
How to Change the Default Port in Spring Boot | Baeldung
July 25, 2025 - Now the server will start on port 8081. And we can do the same if we’re using an application.yml file: ... Both files are loaded automatically by Spring Boot if placed in the src/main/resources directory of a Maven application.