#
Change/Set default port for an API
In Sprint Boot application the default port is 8080, which generally is good, but sometimes we want to use another port number.
This may be done in several ways:
#
by adding in application.properties one line of text :
server.port=8081
#
by adding in application.yaml one line of text :
server:
port : 8081
Info
Specifically, we can create a property file for each environment : application-dev.properties
, application-test.properties
,
application-qa.properties
, application-prod.properties
.
#
using server.port
argument with the java command
Example:
java -jar target/rest-demo-v1.jar --server.port=8081
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.1)
2023-01-07T21:30:11.828+02:00 INFO 19268 --- [ main] c.example.restdemo.RestDemoApplication : Starting RestDemoApplication vv1 using Java 19.0.1 with PID 19268 (D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\rest-demo-v1.jar started by Catalin in D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo)
2023-01-07T21:30:11.830+02:00 INFO 19268 --- [ main] c.example.restdemo.RestDemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-07T21:30:12.643+02:00 INFO 19268 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2023-01-07T21:30:12.652+02:00 INFO 19268 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-01-07T21:30:12.653+02:00 INFO 19268 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-07T21:30:12.725+02:00 INFO 19268 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-01-07T21:30:12.727+02:00 INFO 19268 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 845 ms
2023-01-07T21:30:13.067+02:00 INFO 19268 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2023-01-07T21:30:13.082+02:00 INFO 19268 --- [ main] c.example.restdemo.RestDemoApplication : Started RestDemoApplication in 1.613 seconds (process running for 1.956)
#
using Dserver.port
argument with the java command
java -jar -Dserver.port=8083 target/rest-demo-v1.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.1)
2023-01-07T21:32:03.571+02:00 INFO 1924 --- [ main] c.example.restdemo.RestDemoApplication : Starting RestDemoApplication vv1 using Java 19.0.1 with PID 1924 (D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\rest-demo-v1.jar started by Catalin in D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo)
2023-01-07T21:32:03.574+02:00 INFO 1924 --- [ main] c.example.restdemo.RestDemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-07T21:32:04.388+02:00 INFO 1924 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8083 (http)
2023-01-07T21:32:04.397+02:00 INFO 1924 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-01-07T21:32:04.397+02:00 INFO 1924 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-07T21:32:04.465+02:00 INFO 1924 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-01-07T21:32:04.466+02:00 INFO 1924 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 840 ms
2023-01-07T21:32:04.788+02:00 INFO 1924 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8083 (http) with context path ''
2023-01-07T21:32:04.803+02:00 INFO 1924 --- [ main] c.example.restdemo.RestDemoApplication : Started RestDemoApplication in 1.575 seconds (process running for 1.917)