open-feign调用接口写法总结

open-feign调用接口写法总结

POST请求

post请求,数据格式即Content-Type常见的有以下几种
application/json,form-data,x-www-form-urlencoded等,也可以像GET请求一样,参数直接拼接到url中

1、application/json

普通请求

  • 目标接口代码
@PostMapping("/post1")
public String post1(@RequestBody User user) throws JsonProcessingException {
    return new JsonMapper().writeValueAsString(user);
}
  • openFeign请求代码

参数通过 @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中加参数

  • 目标接口代码
//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;
}
  • openFeign请求代码

有时候接口验签,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);
}

url后面追加参数

  • 目标接口代码
@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;
}
  • openFeign请求代码

除了放到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);
}

url中加参数

  • 目标接口代码
@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;
}
  • openFeign请求代码

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);
}

2、application/x-www-form-urlencoded

  • 目标接口代码
@PostMapping("/post5")
public String post5(User user) throws JsonProcessingException {
    return new JsonMapper().writeValueAsString(user);
}
  • openFeign请求代码
@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);

3、multipart/form-data

  • 目标接口代码
@PostMapping("/post5")
public String post5(User user) throws JsonProcessingException {
    return new JsonMapper().writeValueAsString(user);
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
    @PostMapping(value = "/post5", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
String post6(User user);
}

4、参数全拼接到URL中

  • openFeign请求代码
@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请求

GET请求,参数全放URL中,不建议放BODY,部分浏览器可能会限制不读取body中的数据;
GET请求参数过长的话,也会有问题,适用于参数不长的场景;

  • 目标接口代码
    @GetMapping("/post6")
    public String post6(User user) throws JsonProcessingException {
        return new JsonMapper().writeValueAsString(user);
    }
  • openFeign请求代码
@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注解的,都差不多;后续遇到了再补充;

请登录后发表评论

    没有回复内容