美文网首页
SpringCloud feign 常用功能

SpringCloud feign 常用功能

作者: 杨健kimyeung | 来源:发表于2020-08-21 11:08 被阅读0次

四、GET请求传递POJO

1、问题

Feign 默认不支持GET方法直接绑定POJO的(只支持基本类型),建议使用POST请求的方式传递,

通过调试源码发现的,jdk原生的http连接请求工具类,原来是因为Feign默认使用的连接工具实现类,所以里面发现只要你有body体对象,就会强制的把get请求转换成POST请求。

通常的解决方案:

  1. 把POJO拆散成一个个单独的属性放在方法参数里面(不推荐)
  2. 把方法的参数变成Map传递(不推荐)
  3. 使用Post+@RequestBody
  4. 通过Feign的RequestInterceptor
@FeignClient(value = "test-feign-provider", path = "/user")
public interface UserFeignService {
    @PostMapping
    UserDto find(UserDto userDto);
}

会出现405错误

"feign.FeignException$MethodNotAllowed: status 405 reading UserFeignService#find(UserDto)

2、使用

提供者代码修改成post请求

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @PostMapping("/find")
    public UserDto find(@RequestBody  UserDto userDto) {
        return userDto;
    }

Feign客服端

@FeignClient(value = "test-feign-provider", path = "/user")
public interface UserFeignService {
    @GetMapping("/find")
    UserDto find(@RequestBody UserDto userDto);

日期处理

相关文章

网友评论

      本文标题:SpringCloud feign 常用功能

      本文链接:https://www.haomeiwen.com/subject/hahujktx.html