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注解的,都差不多;后续遇到了再补充;

demo源码地址

https://gitee.com/shuaidawang/openfeign-demo.git

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
出现异常"could not extract response: no suitable httpclient found for response type"通常是由于open-feign微服务间调用时,没有找到合适的http客户端来处理返回的响应类型引起的。 在使用open-feign进行微服务间调用时,我们需要根据实际情况选择适合的http客户端来处理响应。通常open-feign会自动进行http客户端的选择和配置,但有些情况下可能会出现上述异常。 解决该异常的方法有以下几种: 1. 确保引入了适当版本的open-feign和相关依赖库。要使用open-feign,应该在pom.xml文件中添加相应的依赖,并确保其版本与当前使用的spring boot版本兼容。 2. 检查http客户端的配置。可以通过修改application.properties或application.yml文件设置http客户端的配置。例如,在application.properties文件中添加以下配置: ``` feign.httpclient.enabled=true feign.okhttp.enabled=false ``` 这将启用Apache HttpClient并禁用OkHttp客户端。 3. 如果仍然出现异常,可以尝试清除本地maven仓库并重新构建项目。有时候这种异常是由于maven仓库中缓存的库与实际所需版本不一致引起的。 总的来说,解决open-feign微服务间调用异常"could not extract response: no suitable httpclient found for response type"的方法是确保使用了适当的open-feign版本,并根据需要调整http客户端的配置。如果仍然出现异常,可以尝试清除maven仓库并重新构建项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臭小子帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值