How to configure port for a Spring Boot application

前端 未结 30 1241
名媛妹妹
名媛妹妹 2020-11-22 13:36

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.

相关标签:
30条回答
  • 2020-11-22 14:25

    Include below property in application.properties

    server.port=8080
    
    0 讨论(0)
  • 2020-11-22 14:25

    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
    
    0 讨论(0)
  • 2020-11-22 14:25

    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

    0 讨论(0)
  • 2020-11-22 14:26

    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
    

    application.properties file

    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
    

    Change port with a vm arg

    Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application

    0 讨论(0)
  • 2020-11-22 14:26

    also, you can configure port programmatically

    @Configuration
    public class ServletConfig {
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer() {
            return (container -> {
                container.setPort(8012);
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:27

    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
    
    0 讨论(0)
提交回复
热议问题