搭建springboot程序——hello world
-
引入依賴
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-context</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
-
創建類springBootHelloWorld
@RestController
@SpringBootApplication
public class SpringBootHelloWorld{
@RequestMapping("hello")
void helloWorld(){
System.out.println("springboot hello world test");
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorld.class,args);
}
@Bean
public TomcatServletWebServerFactory servletContainer(){
return new TomcatServletWebServerFactory(8090) ;
}
springBoot啓動端口默認爲8080,若端口已經被佔用,則會被拋出異常
-
默認端口修改爲8090
@RestController
@SpringBootApplication
public class SpringBootHelloWorld{
@RequestMapping("hello")
void helloWorld(){
System.out.println("springboot hello world test");
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorld.class,args);
}
@Bean
public TomcatServletWebServerFactory servletContainer(){
return new TomcatServletWebServerFactory(8090) ;
}
}
-
啓動main方法
-
打包
maven pom.xml添加如下內容
<packaging>war</packaging>
<build>
<finalName>springHelloworld</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>