1. 程式人生 > >修改spring boot預設的8080埠為其它埠

修改spring boot預設的8080埠為其它埠

方法一:可以通過實現EmbeddedServletContainerCustomizer介面來實現,程式碼:

import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import 
org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration
public class TestClass extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{ @RequestMapping("/") String home() { return "hi..."; } public static void main(String[] args) { SpringApplication.run(TestClass.class, args); } @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8081); } }
方法二:更加簡單
在src/resource下增加檔案application.properties
加入server.port=8081
即可