Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Change Port for Web Application

Tags:

spring-boot

I am currently trying to create a web application with Spring Boot. I need to host my application to localhost:8081. How do I change the port?

like image 288
Brian Avatar asked May 13 '15 15:05

Brian


4 Answers

By default spring boot uses port 8080, BUT you can change the port just by adding the following code line in your main() like this:

System.getProperties().put( "server.port", *YOUR_PORT_NUMBER_GOES_HERE* );  

e.g

@SpringBootApplication
public class MyClass {
public static void main(String[] args) {
    System.getProperties().put( "server.port", 8181 );  //8181 port is set here
    SpringApplication.run(MyClass.class, args);
}

OR

You can configure it in your application.properties file like so:

server.port=8181

If you DON'T have an application.properties file in your spring-boot application, you can go ahead and create one. Right-click on the src/java/resources folder and go to New-> Other-> General and choose 'File' then name as: application.properties

Any other configurations you might need are listed here https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html. These properties are also configured in the application.properties file.

like image 175
Jet_C Avatar answered Oct 23 '22 01:10

Jet_C


Actually you want to change server.port and you can change it in many different ways as described http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

Examples:

  • in your application.properties (in or outside the jar)
  • command line

    java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar

and much more

like image 23
sodik Avatar answered Oct 23 '22 02:10

sodik


Actually you want to change server.port and you can change it in many different ways as described http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

Put server.port=9000 in your application.properties

like image 6
nijogeorgep Avatar answered Oct 23 '22 02:10

nijogeorgep


In your application.properties file, just add one line

server.port = 8080

And for more configurations you can refer Spring Boot documentation on port

like image 3
Satish Kr Avatar answered Oct 23 '22 01:10

Satish Kr