How to configure port for a Spring Boot application - java

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.

As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with
server.port=8090
For a random port use:
server.port=0
Similarly add application.yml in /src/main/resources/ with:
server:
port: 8090

There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.
Modify application.properties
First you can try the application.properties file in the /resources folder:
server.port = 8090
Modify a VM option
The second way, if you want to avoid modifying any files and checking in something that you only need on your local, you can use a vm arg:
Go to Run -> Edit Configurations -> VM options
-Dserver.port=8090
Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application

Since Spring Boot provides various configuration externalization mechanism (through various PropertySource implementations and/or processors wired into Environment object in order), you can set any property outside of your jar archive through following methods:
Pass property through command line argument as application argument
java -jar <path/to/my/jar> --server.port=7788
From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+)
Define environment variable in U*IX shell:
SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>
By using Java system property:
java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar>
Pass through command line argument:
java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}'
Define JVM system property
java -Dserver.port=7788 -jar <path/to/my/jar>
Define OS environment variable
U*IX Shell
SERVER_PORT=7788 java -jar <path/to/my/jar>
Windows
SET SERVER_PORT=7788
java -jar <path/to/my/jar>
Place property in ./config/application.properties configuration file
server.port=7788
and run:
java -jar <path/to/my/jar>
Place property in ./config/application.yaml
server:
port: 7788
and run:
java -jar <path/to/my/jar>
Place property in ./application.properties
server.port=7788
and run:
java -jar <path/to/my/jar>
Place property in ./application.yaml
server:
port: 7788
and run:
java -jar <path/to/my/jar>
You can combine above methods all together, and the former configuration in the list take precedence over the latter one.
For example:
SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788
The server will start and listen on port 7788.
This is very useful providing default properties in PropertySources with lower precedence (and usually packaged in the archive or coded in the source), and then override it in the runtime environment. And it is the design philosophy of Spring Boot:
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
SERVER_NAME to server.name conversion was done by Relaxed Binding.

Also, you can configure the port programmatically.
For Spring Boot 2.x.x:
#Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public void customize(ConfigurableServletWebServerFactory factory){
factory.setPort(8042);
}
}
For older versions:
#Configuration
public class ServletConfig {
#Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
}

If you would like to run it locally, use this -
mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'
As of Spring Boot 2.0, here's the command that works (clues were here):
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085

You can set port in java code:
HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);
new SpringApplicationBuilder()
.sources(SampleController.class)
.properties(props)
.run(args);
Or in application.yml:
server:
port: 9999
Or in application.properties:
server.port=9999
Or as a command line parameter:
-Dserver.port=9999

In case you are using application.yml add the Following lines to it
server:
port: 9000
and of course 0 for random port.

As explained in Spring documentation, there are several ways to do that:
Either you set the port in the command line (for example 8888)
-Dserver.port=8888 or --server.port=8888
Example : java -jar -Dserver.port=8888 test.jar
Or you set the port in the application.properties
server.port=${port:4588}
or (in application.yml with yaml syntax)
server:
port: ${port:4588}
If the port passed by -Dport (or -Dserver.port) is set in command line then this port will be taken into account. If not, then the port will be 4588 by default.
If you want to enforce the port in properties file whatever the environment variable, you just have to write:
server.port=8888

Include below property in application.properties
server.port=8080

When you need a programatically way of doing it, you can set it during startup:
System.getProperties().put( "server.port", 80 );
SpringApplication.run(App.class, args);
This might help for things like environment dependent port.
Have a nice day

if you are using gradle as the build tool, you can set the server port in your application.yml file as:
server:
port: 8291
If you are using maven then the port can be set in your application.properties file as:
server.port: 8291

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
41.3.3 Working with random ports
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() {
}
}

You can specify port by overriding EmbeddedServletContainerFactory bean within your configuration (java based or xml). There you can specify port for used embedded servlet container. Please, see Spring Boot - Core "Embedded Servlet Container Support" paragraph and example there. Hope this helps.

In application.properties file present in resources:
server.port=8082

There are three ways to do it depending on the application configuration file you are using
a) If you are using application.properties file set
server.port = 8090
b) If you are using application.yml file set server port property in YAML format as given below
server:
port: 8090
c) You can also Set the property as the System property in the main method
System.setProperty("server.port","8090");

There are many other stuffs you can alter in server configuration by changing application.properties.
Like session time out, address and port etc. Refer below post
ref: http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html
I used few of them as below.
server.session.timeout=1
server.port = 3029
server.address= deepesh

Add this in your application.properties file
server.port= 8080

As everyone said, you can specify in application.properties
server.port = 9000 (could be any other value)
If you are using spring actuator in your project, by default it points to
8080, and if you want to change it, then in application.properties mention
management.port = 9001 (could be any other value)

In the application.properties file, add this line:
server.port = 65535
where to place that fie:
24.3 Application Property Files
SpringApplication loads properties from application.properties files
in the following locations and adds them to the Spring Environment:
A /config subdirectory of the current directory
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations
higher in the list override those defined in lower locations).
In my case I put it in the directory where the jar file stands.
From:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

By default spring boot app start with embedded tomcat server start at default port 8080. spring provides you with following different customization you can choose one of them.
NOTE – you can use server.port=0 spring boot will find any unassigned http random port
for us.
1) application.properties
server.port=2020
2) application.yml
server:
port : 2020
3) Change the server port programatically
3.1) By implementing WebServerFactoryCustomizer interface - Spring 2.x
#Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
#Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(2020);
}
}
3.2) By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
#Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
#Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(2020);
}
}
4) By using command line option
java -jar spring-boot-app.jar -Dserver.port=2020

Indeed, the easiest way is to set the server.port property.
If you are using STS as IDE, from version 3.6.7 you actually have Spring Properties Editor for opening the properties file.
This editor provides autocomplete for all Spring Boot properties. If you write port and hit CTRL + SPACE, server.port will be the first option.

By default, spring-web module provides an embedded tomcat server that is running under the port number 8080. If you need to change the port number of the application then go to application.properties file and configure the port number by using server.port property.
server.port= 9876
then your application is running under the port 9876.

Hope this one help
application.properties=>
server.port=8090
application.yml=>
server
port:8090

Using property server.port=8080 for instance like mentioned in other answers is definitely a way to go. Just wanted to mention that you could also expose an environment property:
SERVER_PORT=8080
Since spring boot is able to replace "." for "_" and lower to UPPER case for environment variables in recent versions.
This is specially useful in containers where all you gotta do is define that environment variable without adding/editing application.properties or passing system properties through command line (i.e -Dserver.port=$PORT)

You can add the port in below methods.
Run -> Configurations section
In application.xml add server.port=XXXX

Just have a application.properties in src/main/resources of the project and give there
server.port=****
where **** refers to the port number.

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

Providing the port number in application.properties file will resolve the issue
server.port = 8080
"port depends on your choice, where you want to host the application"

You can set that in application.properties under /src/main/resources/
server.port = 8090

By Default Spring-web module provides an embedded tomcat server runs on port number 8080.
You can change it as follows -
A) If you are using gradle then use can set the property in your application.yml :
server:
port: 8042
B) If you are using maven then you can set the property in your application.properties :
server.port: 8042
C) When you have port in your own config file and want to set it during runtime.
 By implementing WebServerFactoryCustomizer interface - Spring 2.x
#Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
#Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(8042);
}
}
 By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
#Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
#Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(8042);
}
}

Related

In Springboot, is there any way to change the port other than setting it up in application.properties? [duplicate]

This question already has answers here:
How to configure port for a Spring Boot application
(61 answers)
Closed 3 years ago.
I am new to Springboot, just watch a tutorial saying that if I would like to change port, I must do it in the application.properties. I wonder if there is any ways to change the port. Thanks in advance
Programmatic Configuration
We can configure the port programmatically by either setting the specific property when starting the application or by customizing the embedded server configuration.
First, let's see how to set the property in the main #SpringBootApplication class:
#SpringBootApplication
public class CustomApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(CustomApplication.class);
app.setDefaultProperties(Collections
.singletonMap("server.port", "8083"));
app.run(args);
}
}
Next, to customize the server configuration, we have to implement the WebServerFactoryCustomizer interface:
#Component
public class ServerPortCustomizer
implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
#Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8086);
}
}
Note that this applies to Spring Boot 2.x version.
For Spring Boot 1.x, we can similarly implement the EmbeddedServletContainerCustomizer interface.
Using Command Line Arguments
When packaging and running our application as a jar, we can set the server.port argument with the java command:
java -jar spring-5.jar --server.port=8083
Or by using the equivalent syntax:
java -jar -Dserver.port=8083 spring-5.jar
Learn More at: https://www.baeldung.com/spring-boot-change-port
Note: If you have mentioned 8080 in application.properties but you want to run it on 8083 then it will work by giving the port number in command line arguments as like below,
java -jar -Dserver.port=8083 spring-5.jar

Spring boot application with two services on two different ports

I have a spring boot application with two services but i need to run one on port 8080 and the other on 8081. Now i'm developing with sts (Spring tool suite) and i run the application with the option "Run as spring boot app", so i don't know where change server configurations. Someone can help me?
You can't run two different services under the same spring boot application in two different ports. If you want you can move one service to another spring boot application.
But port number will not be same for both services.
you can configure SprintBoot support two ports.The common method configure is use application.properties or the application.yaml as #Madhu Bhat answer. In SprintBoot to configure another port code like this:
create a connector
int port = Integer.parseInt(probePort);
Connector httpConnector = new Connector(HTTP_PROTOCOL);
httpConnector.setPort(port);
Http11NioProtocol handler = (Http11NioProtocol) httpConnector.getProtocolHandler();
handler.setMaxThreads(10);
handler.setMinSpareThreads(4);
//handler.setAddress(InetAddress.getLocalHost());
handler.setAddress(StringTool.getInetAddress());
return httpConnector;
configure connector to
((TomcatEmbeddedServletContainerFactory) container).addAdditionalTomcatConnectors(connector);
The port can be defined on the application.properties or the application.yaml configuration file that you use.
In application.properties file, define it as below:
server.port=8090
Or in case you are using a application.yaml config, define it as below:
server:
port: 8090
If you use Docker (most common solution), you can add the port or its full address as an Environment Variable
docker-compose.yml file like this:
application1:
image: 'application1:latest'
build:
context: ./
container_name: application
environment:
- HOST-APP2=localhost:8082
ports:
- 8091:8080
application2:
image: 'application2:latest'
build:
context: ./
container_name: application
environment:
- HOST-APP1=localhost:8081
ports:
- 8092:8080
or straight in the Dockerfile while building containers
check out here : https://vsupalov.com/docker-arg-env-variable-guide/
it's a good article
you can write below line in application.properties or application.yml
server.port=8080
Sure. You can do it in the application.properties file of spring boot project via setting server.port=number for each service.

How to change port for spring boot [duplicate]

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.
As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with
server.port=8090
For a random port use:
server.port=0
Similarly add application.yml in /src/main/resources/ with:
server:
port: 8090
There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.
Modify application.properties
First you can try the application.properties file in the /resources folder:
server.port = 8090
Modify a VM option
The second way, if you want to avoid modifying any files and checking in something that you only need on your local, you can use a vm arg:
Go to Run -> Edit Configurations -> VM options
-Dserver.port=8090
Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application
Since Spring Boot provides various configuration externalization mechanism (through various PropertySource implementations and/or processors wired into Environment object in order), you can set any property outside of your jar archive through following methods:
Pass property through command line argument as application argument
java -jar <path/to/my/jar> --server.port=7788
From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+)
Define environment variable in U*IX shell:
SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>
By using Java system property:
java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar>
Pass through command line argument:
java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}'
Define JVM system property
java -Dserver.port=7788 -jar <path/to/my/jar>
Define OS environment variable
U*IX Shell
SERVER_PORT=7788 java -jar <path/to/my/jar>
Windows
SET SERVER_PORT=7788
java -jar <path/to/my/jar>
Place property in ./config/application.properties configuration file
server.port=7788
and run:
java -jar <path/to/my/jar>
Place property in ./config/application.yaml
server:
port: 7788
and run:
java -jar <path/to/my/jar>
Place property in ./application.properties
server.port=7788
and run:
java -jar <path/to/my/jar>
Place property in ./application.yaml
server:
port: 7788
and run:
java -jar <path/to/my/jar>
You can combine above methods all together, and the former configuration in the list take precedence over the latter one.
For example:
SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788
The server will start and listen on port 7788.
This is very useful providing default properties in PropertySources with lower precedence (and usually packaged in the archive or coded in the source), and then override it in the runtime environment. And it is the design philosophy of Spring Boot:
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
SERVER_NAME to server.name conversion was done by Relaxed Binding.
Also, you can configure the port programmatically.
For Spring Boot 2.x.x:
#Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public void customize(ConfigurableServletWebServerFactory factory){
factory.setPort(8042);
}
}
For older versions:
#Configuration
public class ServletConfig {
#Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
}
If you would like to run it locally, use this -
mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'
As of Spring Boot 2.0, here's the command that works (clues were here):
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
You can set port in java code:
HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);
new SpringApplicationBuilder()
.sources(SampleController.class)
.properties(props)
.run(args);
Or in application.yml:
server:
port: 9999
Or in application.properties:
server.port=9999
Or as a command line parameter:
-Dserver.port=9999
In case you are using application.yml add the Following lines to it
server:
port: 9000
and of course 0 for random port.
As explained in Spring documentation, there are several ways to do that:
Either you set the port in the command line (for example 8888)
-Dserver.port=8888 or --server.port=8888
Example : java -jar -Dserver.port=8888 test.jar
Or you set the port in the application.properties
server.port=${port:4588}
or (in application.yml with yaml syntax)
server:
port: ${port:4588}
If the port passed by -Dport (or -Dserver.port) is set in command line then this port will be taken into account. If not, then the port will be 4588 by default.
If you want to enforce the port in properties file whatever the environment variable, you just have to write:
server.port=8888
Include below property in application.properties
server.port=8080
When you need a programatically way of doing it, you can set it during startup:
System.getProperties().put( "server.port", 80 );
SpringApplication.run(App.class, args);
This might help for things like environment dependent port.
Have a nice day
if you are using gradle as the build tool, you can set the server port in your application.yml file as:
server:
port: 8291
If you are using maven then the port can be set in your application.properties file as:
server.port: 8291
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
41.3.3 Working with random ports
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() {
}
}
You can specify port by overriding EmbeddedServletContainerFactory bean within your configuration (java based or xml). There you can specify port for used embedded servlet container. Please, see Spring Boot - Core "Embedded Servlet Container Support" paragraph and example there. Hope this helps.
In application.properties file present in resources:
server.port=8082
There are three ways to do it depending on the application configuration file you are using
a) If you are using application.properties file set
server.port = 8090
b) If you are using application.yml file set server port property in YAML format as given below
server:
port: 8090
c) You can also Set the property as the System property in the main method
System.setProperty("server.port","8090");
There are many other stuffs you can alter in server configuration by changing application.properties.
Like session time out, address and port etc. Refer below post
ref: http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html
I used few of them as below.
server.session.timeout=1
server.port = 3029
server.address= deepesh
Add this in your application.properties file
server.port= 8080
As everyone said, you can specify in application.properties
server.port = 9000 (could be any other value)
If you are using spring actuator in your project, by default it points to
8080, and if you want to change it, then in application.properties mention
management.port = 9001 (could be any other value)
In the application.properties file, add this line:
server.port = 65535
where to place that fie:
24.3 Application Property Files
SpringApplication loads properties from application.properties files
in the following locations and adds them to the Spring Environment:
A /config subdirectory of the current directory
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations
higher in the list override those defined in lower locations).
In my case I put it in the directory where the jar file stands.
From:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files
By default spring boot app start with embedded tomcat server start at default port 8080. spring provides you with following different customization you can choose one of them.
NOTE – you can use server.port=0 spring boot will find any unassigned http random port
for us.
1) application.properties
server.port=2020
2) application.yml
server:
port : 2020
3) Change the server port programatically
3.1) By implementing WebServerFactoryCustomizer interface - Spring 2.x
#Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
#Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(2020);
}
}
3.2) By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
#Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
#Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(2020);
}
}
4) By using command line option
java -jar spring-boot-app.jar -Dserver.port=2020
Indeed, the easiest way is to set the server.port property.
If you are using STS as IDE, from version 3.6.7 you actually have Spring Properties Editor for opening the properties file.
This editor provides autocomplete for all Spring Boot properties. If you write port and hit CTRL + SPACE, server.port will be the first option.
By default, spring-web module provides an embedded tomcat server that is running under the port number 8080. If you need to change the port number of the application then go to application.properties file and configure the port number by using server.port property.
server.port= 9876
then your application is running under the port 9876.
Hope this one help
application.properties=>
server.port=8090
application.yml=>
server
port:8090
Using property server.port=8080 for instance like mentioned in other answers is definitely a way to go. Just wanted to mention that you could also expose an environment property:
SERVER_PORT=8080
Since spring boot is able to replace "." for "_" and lower to UPPER case for environment variables in recent versions.
This is specially useful in containers where all you gotta do is define that environment variable without adding/editing application.properties or passing system properties through command line (i.e -Dserver.port=$PORT)
You can add the port in below methods.
Run -> Configurations section
In application.xml add server.port=XXXX
Just have a application.properties in src/main/resources of the project and give there
server.port=****
where **** refers to the port number.
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);
}
}
Providing the port number in application.properties file will resolve the issue
server.port = 8080
"port depends on your choice, where you want to host the application"
You can set that in application.properties under /src/main/resources/
server.port = 8090
By Default Spring-web module provides an embedded tomcat server runs on port number 8080.
You can change it as follows -
A) If you are using gradle then use can set the property in your application.yml :
server:
port: 8042
B) If you are using maven then you can set the property in your application.properties :
server.port: 8042
C) When you have port in your own config file and want to set it during runtime.
 By implementing WebServerFactoryCustomizer interface - Spring 2.x
#Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
#Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(8042);
}
}
 By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
#Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
#Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(8042);
}
}

server.port properties not working after buildin spring boot project

i'm working on spring boot project and all works fine , now i want to build and run the app.
in the application.properties file i set the property server.port = 8090
after building the project using maven i run the following command
java -jar jarfilename.jar but it says the port 8080 is already in use.
i try these commands:
java -jar -Dport=8090 jarfilename.jar
and
java -jar jarfilename.jar --port=8090
but also i got the same message the port 8080 is already in use.
I'm wondering why it takes the port number 8080 and ignore the port number 8090 that i set in the application.properties file.
Note : (I'm using tomcat embedded server) and when i check the folder target/classes.. application.properties i didn't find the property server.port=8090.
can anyone explain to me what' happen exacly?
thanks in advance.
Is the application.properties located at the right location?
Description from Spring.io:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
Use java -jar -Dserver.port=8090 jarfilename.jar to set the port from command line.
Hint, from Spring.io: If you want to use the short term -Dport=8090, you can use server.port=${port:8080} in your application property file.
I encountered the same problem, in my case, I didn't pass the args to SpringApplication.
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class);
}
should be
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
when you create spring boot application using 'spring initializer' select jar file
I know its an old post, but I might know the answer(maybe it will help others): There is a hierarchy with the configuration settings.
The applicaton.properties file is at the bottom(OS env. variables, Java System properties etc. are all above), on the other hand, terminal parameters are at the top.
So when the server initialized, it used the port from a property setting, that is higher in the hierarchy ladder.

Spring Boot not obeying command line arguments

Put together a very basic Spring Boot RESTful backend with some CRUD endpoints. Trying to start it up on my Centos7 server. However I already have an application listening on its default port, 8080, so I have to configure that.
I have tried:
java -jar target/rest-1.0-SNAPSHOT.jar --server.port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --Dserver.port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --Dport=8090
My application.properties contains the line:
server.port=${port:8090}
Spring Boot still starts up the embedded Tomcat container on port 8080. It's not getting the message. What am I missing?
Edit: setting SERVER_PORT=8090 was effective - see below
Try with -Dserver.port=8090 note single -
or
server.port=xxxx in application.properties file
EDIT:-
Check your log, below line will echo in console 2015-10-16 23:13:23.082 INFO 4228 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8090 (http)
VM arg overrides application.properties
If you have a "application.yml" or "application.properties" in the same folder of jar, the jar will take preference of the external configuration file.
Doesnt make sense for me, but its true. The springboot has higher priority to external configuration file instead the internal one.
We have four different ways to change default port.
Using application.properties or application.yml
Implementing EmbeddedServletContainerCustomizer interface
Using SpringApplication class
Change port directly through command line
For more details, visit below
http://www.javainterviewpoint.com/spring-boot-change-embedded-tomcat-default-port/

Categories

Resources