一、前言
專案配置完之後,接著就是寫介面了,那我們們就開始吧。
二、專案配置補充知識點
上篇文章寫的是關於專案屬性配置的一些知識,這裡針對上次遺忘內容進行補充如下:
2.1、獲取配置檔案的值
- 在
application.yml
檔案中,示例內容如下:
server:
port: 8888
name: xiaoqiang
age: 11
注意:這裡關於yml
檔案的書寫,使用@Value
取值時,配置檔案的欄位一定要頂格寫,如:name: xiaoqiang
,因為空格會認為是某個物件的屬性,這裡一定要注意。
- 利用
@Value
註解取值
示例介面如下:
@RestController
public class HelloController {
@Value("${name}")
private String name;
@GetMapping("/say")
public String say(){
return "hi ,"+name;
}
}
- 訪問
say
介面,就可以獲取到值
GET http://localhost:8888/say
HTTP/1.1 200
Content-Type: application/json
Content-Length: 15
Date: Thu, 26 Aug 2021 07:33:02 GMT
Keep-Alive: timeout=60
Connection: keep-alive
hi ,xiaoqiang
2.2、使用自定義配置類
如果屬性很多,我們每個屬性都需要寫,顯得有些費事,我們可以利用自定義配置類進行獲取
- 修改yml 檔案
server:
port: 8888
name: xiaoqiang
#學生物件的屬性
student:
name: alex
age: 18
- 建立
Student.java
示例程式碼如下:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author longrong.lang
* @version 1.0
* @description
* @date 2021/8/17 21:16
*/
@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private int age;
}
- 在
pom
檔案中引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 介面示例
@RestController
public class HelloController {
@Resource
private Student student;
@GetMapping("/sayToStudent")
public String sayToStudent(){
return "hi,"+student.getName();
}
}
- 驗證結果
GET http://localhost:8888/sayToStudent
HTTP/1.1 200
Content-Type: application/json
Content-Length: 7
Date: Thu, 26 Aug 2021 07:49:24 GMT
Keep-Alive: timeout=60
Connection: keep-alive
hi,alex
Response code: 200; Time: 165ms; Content length: 7 bytes
三、神祕的controller
可能很多同學會好奇,介面都在哪寫? controller
裡寫呀。
3.1、關於controller
用於定義介面,是請求的入口
3.2、常用註解的使用
@RestController
註解用於宣告返回文字資料,一般返回JSON@Controller
註解用於宣告返回介面@RestController = @Controller + ResponseBody
3.3、介面的常用寫法及引數使用
使用不同路徑訪問同一個方法
示例程式碼如下:
/**
* 使用不同路徑訪問同一個方法
* @return
*/
@RequestMapping(method= RequestMethod.GET,value = {"/hello","/hi"})
//@GetMapping({"/hello","/hi"})
public String sayHello(){
return "hello,Spring Boot!";
}
說明:
@GetMapping("/hello")
等價於
@RequestMapping(value="/hello", method=RequestMethod.GET)
@PathVariable:
的使用
示例程式碼如下:
@RestController
public class HelloController {
/**
* @PathVariable 使用示例
* @param age
* @return
*/
@GetMapping("/getStudentAge/{age}")
public int getAge(@PathVariable("age") Integer age){
return age;
}
}
訪問http://localhost:8888/getStudentAge/111
, 結果如下
GET http://localhost:8888/getStudentAge/111
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 26 Aug 2021 08:07:18 GMT
Keep-Alive: timeout=60
Connection: keep-alive
111
Response code: 200; Time: 193ms; Content length: 3 bytes
@RequestParam的使用
正常請求,示例程式碼如下:
@RestController
public class HelloController {
/**
* @RequestParam使用示例
* @param name
* @return
*/
@GetMapping("/getStudentName")
public String getStudentName(@RequestParam String name){
return "name :"+name;
}
}
訪問http://localhost:8888/getStudentName?name=111
, 結果如下:
GET http://localhost:8888/getStudentName?name=111
HTTP/1.1 200
Content-Type: application/json
Content-Length: 9
Date: Thu, 26 Aug 2021 08:14:13 GMT
Keep-Alive: timeout=60
Connection: keep-alive
name :111
Response code: 200; Time: 172ms; Content length: 9 bytes
設定引數非必須的,並且設定上預設值
@RestController
public class HelloController {
/**
* @RequestParam 設定引數非必須的,並且設定上預設值
* @param name
* @return
*/
@GetMapping("/getStudentInfo")
public String getStudentInfo(@RequestParam(value = "name",required = true,defaultValue = "rongrong") String name,@RequestParam(value = "age",required = true,defaultValue = "19")int age){
return "name :"+name+" \t age: "+age;
}
}
訪問http://localhost:8888/getStudentInfo?name=111&age=11
, 結果如下:
GET http://localhost:8888/getStudentInfo?name=111&age=11
HTTP/1.1 200
Content-Type: application/json
Content-Length: 19
Date: Thu, 26 Aug 2021 08:29:12 GMT
Keep-Alive: timeout=60
Connection: keep-alive
name :111 age: 11
Response code: 200; Time: 48ms; Content length: 19 bytes
到此,Controller中介面的寫法介紹完成。