feien中get请求无法传递复杂对象参数的问题

feien中get请求无法传递复杂对象参数的问题

  • 在使用SpringCloud搭建互联网项目时发现的问题

  • 使用Feign进行get方法调用

    • 可以传递简单参数

      //路径 :  http://localhost:10010/findUser?uid=1
      @GetMapping("/findStudentBySid")
      public BaseResult findStudentBySid(@RequestParam("sid") string uid);
      
    • 无法传递复杂参数(对象)------报错

      //路径 :  http://localhost:10010/findUser?uid=1$username=jack&password=1234
      @GetMapping("/findStudentBySid")
      public BaseResult findStudentBySid(User user);
      
      • 原因 : feign使用get进行调用时,如果遇到复杂参数会自动转化成Post请求
  • 解决方案

    • 不使用路径进行复杂参数的传递,使用Json进行数据传递

      {
          "uid":"1",
      	"username":"jack",
      	"password":"1234"
      }
      
    • 使用@RequestBody进行参数传递

      //路径 :  http://localhost:10010/findUser
      @GetMapping("/findStudentBySid")
      public BaseResult findStudentBySid(@RequestBody User user);
      
    • 改用Post请求

      //路径 :  http://localhost:10010/findUser
      @PostMapping("/findStudentBySid")
      public BaseResult findStudentBySid(@RequestBody User user);
      
发布了31 篇原创文章 · 获赞 0 · 访问量 178

猜你喜欢

转载自blog.csdn.net/weixin_46759279/article/details/105713448
今日推荐