尋找寫代碼感覺(三)之使用 Spring Boot 編寫接口
- 2021 年 8 月 28 日
- 筆記
- Spring Boot
一、前言
項目配置完之後,接着就是寫接口了,那咱們就開始吧。
二、項目配置補充知識點
上篇文章寫的是關於項目屬性配置的一些知識,這裡針對上次遺忘內容進行補充如下:
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 //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 //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;
}
}
訪問//localhost:8888/getStudentAge/111
, 結果如下
GET //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;
}
}
訪問//localhost:8888/getStudentName?name=111
, 結果如下:
GET //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;
}
}
訪問//localhost:8888/getStudentInfo?name=111&age=11
, 結果如下:
GET //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中接口的寫法介紹完成。