1. 程式人生 > 實用技巧 >02、SpringBoot2入門

02、SpringBoot2入門

1、系統要求

  • Java 8 & 相容java14 .
  • Maven 3.3+
  • idea 2019.1.2

1.1、maven設定

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

2、HelloWorld

需求:瀏覽傳送/hello請求,響應 Hello,Spring Boot 2

2.1、建立maven工程

2.2、引入依賴

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

  

2.3、建立主程式

/**
 * 主程式類
 * @SpringBootApplication:這是一個SpringBoot應用
 */
@SpringBootApplication
public class MainApplication {

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

  

2.4、編寫業務

@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }


}

  

2.5、測試

直接執行main方法

2.6、簡化配置

application.properties

server.port=8888

  

2.7、簡化部署

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  

把專案打成jar包,直接在目標伺服器執行即可。

注意點:

  • 取消掉cmd的快速編輯模式