一、創建及運行方式
1. 從官網導入:
注意:由于我的java版本是1.8;所以選中了spring2.7.14;如果你的java版本是9及以上,選中spring3相關的同時Java 版本也要對應起來
2. 創建第一個get請求
?創建Controller? package及類,創建以下的代碼:
package com.example.aitestmini.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;//與前端請求的類
@RestController
public class firstController {//需求:get請求,路徑/first, 前端顯示hello springboot@GetMapping("/first")String first(){return "Hello Spring!";// http://localhost:8080/first}
}
運行:
運行AitestMiniApplication的run()方法
在瀏覽器訪問http://localhost:8080/first,應該是展示hello spring
8080端口被占用,使用以下命令查看占用端口的pid:
lsof -i :8080
執行下面命令kill調進程:
kill -9 pid
3. 打包
mvn package
執行以上命令,會在target/目錄下自動生成jar包
4. 運行方式
方式一:
之前已經說過了,執行run()方法
方式二:
java -jar target/aitest-mini-0.0.1-SNAPSHOT.jar
運行以上命令,jar是剛才打出來的jar包
方式三:
mvn spring-boot:run
執行以上命令
二、端口管理
1. 常見端口實名方式
如果配置文件不聲明端口,默認按照80801. application.properties配置文件管理端口:
server.port=80812. application.yml配置文件管理端口:
server:port: 80823. 運行命令聲明端口
mvn clean package
java -jar -Dserver.port=8083 target/aitest-mini-0.0.1-SNAPSHOT.jar
2. 不同環境配置不同的端口
- 針對環境創建不同的配置文件
?
?
?3. 運行不同環境端口的方式
1. application.properties管理:spring.profiles.active=dev2. application.yml管理
spring:profiles:active: dev3. 在pom文件管理<profiles><profile><id>dev</id><properties><profilesActive>dev</profilesActive></properties></profile><profile><id>qa</id><properties><profilesActive>qa</profilesActive></properties><!-- 默認qa環境--><activation><activeByDefault>true</activeByDefault></activation></profile></profiles>添加上面的依賴,在application.properties管理:
spring.profiles.active=@profilesActive@
三、Get請求Demo
1. 普通get請求的聲明方式
1. 方法名前面使用:@GetMapping("/first")
2. 方法名前面使用:@RequestMapping(path = "/first",method = RequestMethod.GET)
@RestController
public class firstController {//需求:get請求,路徑/first, 前端顯示hello springboot
// @GetMapping("/first")@RequestMapping(path = "/first",method = RequestMethod.GET)String first(){return "Hello Spring!";// http://localhost:8080/first}
}
2. 帶有參數的Get請求
需求:
http://localhost:8081/topic/{id}
對應瀏覽器顯示地址:{id}為內容
// 如果不想寫@PathVariable("id")里面的id,需要保證傳入的id->sid
@RestController
public class BaseGetWithIdController {@GetMapping("/topic/{id}")String getTopic(@PathVariable("id") String tid){return "請求的id為 " + tid + " 的內容!!!";}
}
3. 在queryParam拼接
需求:http://localhost:8081/native?s={sid}
對應瀏覽器顯示地址:這是一個本國地址為:{sid}的內容
@GetMapping("/native")String getNative(@RequestParam("s") String sid){return "這是一個本國地址為:" + sid + " 的內容!";}
升級:
如果當s=66,打印不一樣的內容:
@GetMapping("/native")String getNative(@RequestParam("s") String sid){return "這是一個本國地址為:" + sid + " 的內容!";}@GetMapping(path = "/native", params = {"s=66"})String getNative1(@RequestParam("s") String sid){return "這是一個本國地址為:" + sid + " 的內容!getNative1";}
4. 在controller前面加@RequestMapping("/t")
效果:相當于全部的鏈接前面加了/t會被自動識別
代碼:
@RequestMapping(path = "/first",method = RequestMethod.GET)String first(){return "Hello Spring!";// http://localhost:8080/first}
5.?PathVariable參數非必填
//1. @PathVariable(value = "did",required = false)中的required默認是true,可以設置為false
//2. 聲明完成之后, 對應的訪問路徑會有2中,需在GetMapping里面說明value = {"/topic/{did}/u","/topic/u"}@GetMapping(value = {"/topic/{did}/u","/topic/u"})String getTopic1(@PathVariable(value = "did",required = false) String topid,@RequestParam(defaultValue = "66") int sid){return "請求的id為 " + topid + " 的內容!!!并且參數sid為" + sid + "的內容!!!";}//http://localhost:8081/t/topic/99/u//http://localhost:8081/t/topic/u
6.?RequestParam非必填及提供默認值
@GetMapping("/top/{city}/{year}")String getRUIWithPara(@PathVariable int year,@PathVariable String city,@RequestParam(defaultValue = "GDP",required = false) String desc,@RequestParam(defaultValue = "45666") int money){return "{"+ year + "}年{" + city + "}人均{" + desc + "}為:{" + money + "}";}
1. desc非必填,但是GetMapping的值只有一個,因為desc的類型不是PathVariable
2. @RequestParam(defaultValue = "GDP",required = false) String desc說明RequestParam的required如果為false可以非必填