how to configure port

Looking for way how to configure port in Spring boot app:

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

That is all sources :)


As said in docs either set server.port as system property using command line option to jvm --server.port=8090 or add application.properties in /src/main/resources/ with

server.port = 8090

For random port use

server.port=0

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:

application.properties文件

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

用vm arg改变端口

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


另外,您可以通过编程来配置端口

@Configuration
public class ServletConfig {
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(8012);
        });
    }
}
链接地址: http://www.djcxy.com/p/8572.html

上一篇: 多部分文件上传Spring Boot

下一篇: 如何配置端口