🍀 请求方法
Forest 使用不同的请求注解来标识某个接口方法来进行发送不同类型的请求,其支持的HTTP方法如下表所示:
| HTTP 请求方法 | 请求注解 | 描述 | 
|---|---|---|
| GET | @Get、@GetRequest |  获取资源 | 
| POST | @Post、@PostRequest |  传输实体文本 | 
| PUT | @Put、@PutRequest |  上传资源 | 
| HEAD | @HeadRequest |  获取报文首部 | 
| DELETE | @Delete、@DeleteRequest |  删除资源 | 
| OPTIONS | @Options、@OptionsRequest |  询问支持的方法 | 
| TRACE | @Trace、@TraceRequest |  追踪路径 | 
| PATCH | @Patch、@PatchRequest |  更新资源的某一部分 | 
| 不定方法 | @Request |  可动态传入HTTP方法 | 
# GET 请求
使用@Get注解或@GetRequest注解
@Get("http://localhost:8080/hello")
String simpleGet1();
@GetRequest("http://localhost:8080/hello")
String simpleGet2();
 1
2
3
4
5
2
3
4
5
# POST 请求
使用@Post注解或@PostRequest注解
@Post("http://localhost:8080/hello")
String simplePost1();
@PostRequest("http://localhost:8080/hello")
String simplePost2();
 1
2
3
4
5
2
3
4
5
# PUT 请求
使用@Put注解或@PutRequest注解
@Put("http://localhost:8080/hello")
String simplePut1();
@PutRequest("http://localhost:8080/hello")
String simplePut2();
 1
2
3
4
5
2
3
4
5
# HEAD 请求
使用@HeadRequest注解
为了避免于@Header注解产生歧义和混淆,Forest 没有提供@Head注解
@HeadRequest("http://localhost:8080/hello")
String simpleHead();
 1
2
2
# DELETE 请求
使用@Delete注解或@DeleteRequest注解
@Delete("http://localhost:8080/hello")
String simpleDelete1();
@DeleteRequest("http://localhost:8080/hello")
String simpleDelete2();
 1
2
3
4
5
2
3
4
5
# OPTIONS 请求
使用@Options注解或@OptionsRequest注解
@Options("http://localhost:8080/hello")
String simpleOptions1();
@OptionsRequest("http://localhost:8080/hello")
String simpleOptions2();
 1
2
3
4
5
2
3
4
5
# TRACE 请求
使用@Trace注解或@TraceRequest注解
@Trace("http://localhost:8080/hello")
String simpleTrace1();
@TraceRequest("http://localhost:8080/hello")
String simpleTrace2();
 1
2
3
4
5
2
3
4
5
# PATCH 请求
使用@Patch注解或@PatchRequest注解
@Patch("http://localhost:8080/hello")
String simplePatch1();
@PatchRequest("http://localhost:8080/hello")
String simplePatch2();
 1
2
3
4
5
2
3
4
5
# 动态 HTTP 请求方法
若不想在接口定义的时候直接定死为某个具体的 HTTP 请求方法,而是想从全局变量或方法参数中动态传入
可以使用 @Request 请求注解
/**
 * 通过在 @Request 注解的 type 属性中定义字符串模板
 * 在字符串模板中引用方法的参数
 */
@Request(
    url = "http://localhost:8080/hello",
    type = "{type}"
)
String simpleRequest(@Var("type") String type);
 1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
在调用该方法时通过参数传入 HTTP 请求方法类型(字符串类型,大小写不敏感)
// POST 请求
String result1 = simpleRequest("post");
// DELETE 请求
String result2 = simpleRequest("DELETE");
 1
2
3
4
2
3
4
帮助我们改善此文档  (opens new window)
  上次更新: 2025/07/02, 10:43:35