一、環境
- Idea 2020.1
- JDK 1.8
- maven
二、目的
初識Spring Boot,spring boot入門的第一個示例, gitHub地址:https://github.com/ouyushan/ouyushan-spring-boot-samples三、步驟
3.1、點擊File -> New Project -> Spring Initializer,點擊next
3.2、在對應地方修改自己的專案資訊

3.3、修改后如下,點擊next

3.4、選擇Web依賴,選中Spring Web,可以選擇Spring Boot版本,本次默認為2.2.6,點擊Next

3.5、編輯工程名和專案路徑,確定后點擊Finish完成

3.6、完成新建工程

四、代碼分析
4.1、運行專案 執行SpringBootBeginHelloApplication下的main方法

4.2、測驗運行
瀏覽器訪問 http://localhost:8080/ 顯示結果如下,表示該web專案正常啟動

4.3、新增測驗介面

新建包controller,然后建立HelloController類,具體代碼如下:
1 package org.ouyushan.springboot.begin.controller; 2 3 import org.springframework.web.bind.annotation.GetMapping; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 /** 8 * @Description 9 * @Author ouyushan 10 * @Email [email protected] 11 * @Date 2020/4/27 12 */ 13 14 @RestController 15 @RequestMapping("/springboot") 16 public class HelloController { 17 18 @GetMapping("/hello") 19 public String hello() { 20 return "Hello,Spring Boot!"; 21 } 22 23 }View Code
4.4、新介面測驗
再次啟動程式后瀏覽器訪問: http://localhost:8080/springboot/hello 介面請求回傳如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.6.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>org.ouyushan</groupId> 12 <artifactId>spring-boot-begin-hello</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>spring-boot-begin-hello</name> 15 <description>Hello project for Spring Boot</description> 16 17 <!--指定jdk版本--> 18 <properties> 19 <java.version>1.8</java.version> 20 </properties> 21 22 <dependencies> 23 <!--指定spring boot web依賴--> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-test</artifactId> 32 <scope>test</scope> 33 <exclusions> 34 <exclusion> 35 <groupId>org.junit.vintage</groupId> 36 <artifactId>junit-vintage-engine</artifactId> 37 </exclusion> 38 </exclusions> 39 </dependency> 40 </dependencies> 41 42 <build> 43 <plugins> 44 <plugin> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-maven-plugin</artifactId> 47 </plugin> 48 </plugins> 49 </build> 50 51 </project>View Code
五、知識點
5.1、spring boot 專案可通過以下方式指定jdk版本
1 <!--指定jdk版本--> 2 <properties> 3 <java.version>1.8</java.version> 4 </properties>View Code
5.2、spring boot 埠默認是8080
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/176783.html
標籤:Java
下一篇:如何短時間內快速通過Java面試
