Spring Boot: Change Port for Web Application

后端 未结 6 1688
无人共我
无人共我 2021-02-20 08:41

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?

6条回答
  •  面向向阳花
    2021-02-20 09:17

    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.

提交回复
热议问题