Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 525 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to configure port for a Spring Boot application

#11
You can set that in application.properties under /src/main/resources/

server.port = 8090
Reply

#12
"**server.port=8080**" will only works if your running application as a jar through main method,

This configuration will not work if your running this application as a war file through tomcat container.
Reply

#13
If you are going to run apps as jar file in command environment, just type "SERVER_PORT=*** " as prefix. The full command to execute will look like below:

SERVER_PORT=8080 java -jar ***.jar
If you wanna run app in background in Linux, command with 'nohup' will look like below:

SERVER_PORT=8080 nohup java -jar ***.jar &
Reply

#14
The default port is : 8080 but we can customize the port number in application.properties
as shown below


spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port = 5050 -- #here we can give any port number.


Reply

#15
In `application.properties` file present in resources:

server.port=8082

Reply

#16
Just have a `application.properties` in `src/main/resources` of the project and give there

server.port=****
where `****` refers to the port number.
Reply

#17
This worked for me :

Added a custom container class :

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(8888);
}

}

But this was still not using port 8888.

Then I set "scanBasePackages" property like this on "@SpringBootApplication" class on my main method: `(scanBasePackages = {"custom container package"})`

@SpringBootApplication(scanBasePackages = {"com.javabrains.util"})
public class CourseApiApp {

public static void main (String args []) {
SpringApplication.run(CourseApiApp.class, args);
}
}

And it started picking up port set in Custom Container.
Reply

#18
To extend other answers:

There is a section in the docs for testing which explains how to configure the port on integration tests:


- [41.3 Testing Spring Boot applications][1]
- [41.3.3 Working with random ports][2]

---

At integration tests, the port configuration is made using the annotation `@SpringBootTest` and the `webEnvironment` values.

---
### Random port:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

You can inject the value using `@LocalServerPort` which is the same as `@Value("${local.server.port}")`.

- Example:

Random port test configuration:


@RunWith(SpringRunner.class
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ExampleTest {
...
@LocalServerPort //to inject port value
int port;
}

---

### Defined port:

@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)

It takes the value from `server.port` if is defined.

- If is defined using `@TestPropertySource(properties = "server.port=9192")`, it overrides other defined values.
- If not, it takes the value from `src/test/resources/application.properties` (if exists).
- And finally, if it is not defined it starts with the default `8080`.

Example:

Defined port test configuration:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=9192")
public class DemoApplicationTests {

@Test
public void contextLoads() {
}

}


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#19
Mostly springboot runs on `port:8080` because of embedded Tomcat. In some it may throw an error `port 8080 already in use`. To avoid this kind of issues we can config the server port.
Using application.properties
-
add `server.port=9898`
On runtime config
-
run your application with below arguments.

`spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'`
Reply

#20
1.1 Update via a properties file.

**/src/main/resources/application.properties**

server.port=8888

**Update via a yaml file.**

server:

port: 8888

**EmbeddedServletContainerCustomizer**

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {

container.setPort(8888);

}

}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through