寻找写代码感觉(三)之使用 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 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中接口的写法介绍完成。
优秀不够,你是否无可替代
软件测试交流QQ群:721256703,期待你的加入!!
欢迎关注我的微信公众号:软件测试君
![](https://www.cnblogs.com/images/cnblogs_com/longronglang/1061549/o_QQ%E6%88%AA%E5%9B%BE20190728134401.jpg)