Java 如何使用 spring boot 更改 embebed-tomcat 默认端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26790321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:17:25  来源:igfitidea点击:

How to change embebed-tomcat default port using spring boot?

javatomcatspring-boot

提问by Juan Henao

I am using spring-boot with maven, this is my configuration class:

我在 maven 中使用 spring-boot,这是我的配置类:

package hello;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

When the app starts show this line in console:

当应用程序启动时,在控制台中显示这一行:

2014-11-06 17:00:55.102  INFO 4669 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http

I want to change the TomcatEmbedded port to 8081 for the case. Thanks :D

对于这种情况,我想将 TomcatEmbedded 端口更改为 8081。感谢:D

采纳答案by kryger

Set the value via the server.portproperty, just like explained in the documentation, e.g.:

通过server.port属性设置值,就像文档中解释的那样,例如:

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'

回答by robin

Use double quote:

使用双引号:

mvn spring-boot:run -Drun.jvmArguments="-Dserver.port=8081"

mvn spring-boot:run -Drun.jvmArguments="-Dserver.port=8081"

回答by bpjoshi

There are 3-4 ways to change it. Add application.properties under

有 3-4 种方法可以更改它。在下面添加 application.properties

src/main/resources/ 

and add the property as below to the file:

并将如下属性添加到文件中:

server.port = 8084

For other ways to change, go through this link.

如需其他更改方式,请访问此链接

Spring official documentation linkfor the same.

相同的 Spring 官方文档链接

回答by grey zeng

Write this in application.yml:

在 application.yml 中写入:

server:
  port: [your port]

for example

例如

server:
  port:8888

to change the default port to 8888

将默认端口更改为 8888