使用POST方式:
public interface MyClient {/*** 通过 @Request 注解的 type 参数指定 HTTP 请求的方式。*/@Request(url = "http://localhost:8080/hello",type = "POST")String simplePost();/*** 使用 @Post 注解,可以去掉 type = "POST" 这行属性*/@Post("http://localhost:8080/hello")String simplePost();/*** 使用 @PostRequest 注解,和上面效果等价*/@PostRequest("http://localhost:8080/hello")String simplePost();}
除了GET和POST,也可以指定成其他几种 HTTP 请求方式(PUT, HEAD, OPTIONS, DELETE)。
其中type属性的大小写不敏感,写成POST和post效果相同。
// GET请求@Request(url = "http://localhost:8080/hello",type = "get")String simpleGet();// POST请求@Request(url = "http://localhost:8080/hello",type = "post")String simplePost();// PUT请求@Request(url = "http://localhost:8080/hello",type = "put")String simplePut();// HEAD请求@Request(url = "http://localhost:8080/hello",type = "head")String simpleHead();// Options请求@Request(url = "http://localhost:8080/hello",type = "options")String simpleOptions();// Delete请求@Request(url = "http://localhost:8080/hello",type = "delete")String simpleDelete();
另外,可以用@GetRequest, @PostRequest等注解代替@Request注解,这样就可以省去写type属性的麻烦了。
// GET请求@Get("http://localhost:8080/hello")String simpleGet();// GET请求@GetRequest("http://localhost:8080/hello")String simpleGetRequest();// POST请求@Post("http://localhost:8080/hello")String simplePost();// POST请求@PostRequest("http://localhost:8080/hello")String simplePostRequest();// PUT请求@Put("http://localhost:8080/hello")String simplePut();// PUT请求@PutRequest("http://localhost:8080/hello")String simplePutRequest();// HEAD请求@HeadRequest("http://localhost:8080/hello")String simpleHead();// Options请求@Options("http://localhost:8080/hello")String simpleOptions();// Options请求@OptionsRequest("http://localhost:8080/hello")String simpleOptionsRequest();// Delete请求@Delete("http://localhost:8080/hello")String simpleDelete();// Delete请求@DeleteRequest("http://localhost:8080/hello")String simpleDeleteRequest();
如上所示,请求类型是不是更一目了然了,代码也更短了。
