post请求,数据格式即Content-Type常见的有以下几种
application/json,form-data,x-www-form-urlencoded等,也可以像GET请求一样,参数直接拼接到url中
@PostMapping("/post1")
public String post1(@RequestBody User user) throws JsonProcessingException {
return new JsonMapper().writeValueAsString(user);
}
参数通过
@RequestBody
标识
请求头_Content-Type _在@Headers
注解中指定
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
@Headers({"Content-Type: application/json"})
@PostMapping("/post1")
String post1(@RequestBody User user);
}
//header中增加参数authorization
@PostMapping("/post2")
public String post2(@RequestBody User user, HttpServletRequest request) throws JsonProcessingException {
String authorization = request.getHeader("authorization");
return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization;
}
有时候接口验签,header中需要追加一些参数;通过
@RequestHeader
注解实现
例如header中增加参数authorization
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
@Headers({"Content-Type:application/json","Authorization:{authorization}"})
@PostMapping("/post2")
String post2(@RequestHeader(name = "authorization")String authorization,
@RequestBody User user);
}
@PostMapping("/post3")
public String post4(@RequestBody User user, HttpServletRequest request,
@RequestParam("gg") String gg) throws JsonProcessingException {
String authorization = request.getHeader("authorization");
return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization + ", gg="+ gg;
}
除了放到body中的参数,还能直接在url后面追加参数,
@RequestParam
注解
例如增加参数gg
http://xxxxxxxx?gg=xxxx
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
@Headers({"Content-Type:application/json","Authorization:{authorization}"})
@PostMapping("/post3")
String post3(@RequestHeader(name = "authorization") String authorization,
@RequestParam("gg") String gg,
@RequestBody User user);
}
@PostMapping("/post4/{userId}")
public String post4(@RequestBody User user, HttpServletRequest request,
@PathVariable String userId,
@RequestParam("gg") String gg) throws JsonProcessingException {
String authorization = request.getHeader("authorization");
return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization + ", userId=" + userId + ", gg="+ gg;
}
url以及url后边追加参数 ,参数用
@PathVariable
注解
注意:这里的gg
参数别用@RequestParam
注解;
例如:新增参数userId
, 其中gg
参数跟上面的写法对比下;
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
@Headers({"Content-Type:application/json","Authorization:{authorization}"})
@PostMapping("/post3/{userId}?gg={gg}")
String post4(@RequestHeader(name = "authorization")String authorization,
@PathVariable("userId") String userId,
@PathVariable("gg") String gg,
@RequestBody User user);
}
@PostMapping("/post5")
public String post5(User user) throws JsonProcessingException {
return new JsonMapper().writeValueAsString(user);
}
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
@Headers({"Content-Type: application/x-www-form-urlencoded;charset=UTF-8"})
@PostMapping("/post5")
String post5(@RequestBody MultiValueMap<String, Object> user);
}
//使用MultiValueMap的实现类LinkedMultiValueMap
//用add方法添加参数
LinkedMultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("name","臭小子");
multiValueMap.add("sex", "男");
feignApiInteferce.post5(multiValueMap);
@PostMapping("/post5")
public String post5(User user) throws JsonProcessingException {
return new JsonMapper().writeValueAsString(user);
}
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
@PostMapping(value = "/post5", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
String post6(User user);
}
@FeignClient(name = "feignApiClient", url = "http://localhost:${server.port}/produce")
public interface FeignApiInteferce{
@PostMapping("/post5")
String post7(@RequestParam("name") String name, @RequestParam("sex") String sex);
}
GET请求,参数全放URL中,不建议放BODY,部分浏览器可能会限制不读取body中的数据;
GET请求参数过长的话,也会有问题,适用于参数不长的场景;
@GetMapping("/post6")
public String post6(User user) throws JsonProcessingException {
return new JsonMapper().writeValueAsString(user);
}
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
//一个个传参
@GetMapping("/post6")
String post8(@RequestParam("name") String name, @RequestParam("sex") String sex);
//对象传参用 `@SpringQueryMap` 注解
@GetMapping(value = "/post6")
String post9(@SpringQueryMap User user);
}
这里简单总结了一些常见的写法;其它还有像官网的写法通过@RequestLine
和@param
注解的,都差不多;后续遇到了再补充;
https://gitee.com/shuaidawang/openfeign-demo.git