1. 程式人生 > 其它 >SpringBoot-hello world

SpringBoot-hello world

SpringBoot2使用流程:

  1.maven設定


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.hrf</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>


<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

  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> </project>

  3.建立主程式

/**
 * 主程式類
 * SpringBootApplication:告訴SpringBoot這是一個SpringBoot應用
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

  4.編寫業務

/*
*@RestContorller是@Conretller和@ResponseBody兩個註釋相加的效果一致
*/
@RestController
public class HelloController { @RequestMapping("/hello") public String handle01(){ return "hello,SpringBoot2"; } }

  5.測試方法

    直接執行主程式類

  6.SpringBoot配置檔案

    (application.properties檔名和字尾都不能改變)

    配置檔案可以修改整個SpringBoot應用

server.port=8888

  7.簡化部署

    (在maven檔案中)    報紅的話maven清理一下重新打包即可

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

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