• Springcloud中Feign传递参数


    传递单个参数:

    单个参数的传值有两种方式,第一种
    使用@RequestParam/@PathVariable进行传值

    客户端feign调用接口(@RequestParam)

    1. @RequestMapping("/ct/selectOne")
    2. Customer selectOne(@RequestParam("id") Integer id);

    服务提供端

    1. @RequestMapping("selectOne")
    2. public Customer selectOne(Integer id) {
    3. return this.customerService.queryById(id);
    4. }

    客户端feign调用接口(@PathVariable)

    1. @GetMapping("/admin/selectOne/{id}")
    2. String selectOne(@PathVariable("id") Integer id);

    服务提供端

    1. @RequestMapping("selectOne/{id}")
    2. @HystrixCommand(fallbackMethod = "HystrixqueryById")
    3. public Admin selectOne(@PathVariable("id") Integer id) {
    4. Admin bean = adminService.queryById(id);
    5. if(bean == null){
    6. throw new RuntimeException("id:"+id+"没有找到该id的用户");
    7. }
    8. return bean;
    9. }

    注意:
    1、在使用@RequestParam/@PathVariable进行传值时,一定要注意,需要绑定参数,如@RequestParam(“id”)绑定id,不然会报错

    2、@PathVariable是获取url上数据的,@RequestParam获取请求参数的(包括post表单提交)

    传递多个参数:
    多个参数的传值可以使用多个@RequestParam来进行传参

    客户端feign调用接口
     

    1. @RequestMapping("/ct/upload")
    2. Customer upload(@RequestParam("newFileName") String newFileName,
    3. @RequestParam("id") int id);

    服务提供端

    1. @RequestMapping("upload")
    2. public Customer upload(String newFileName,int id) throws IOException {
    3. System.out.println("进入提供者-图片上传");
    4. //设置图片上传路径,是目标文件夹的路径
    5. // 保存到数据库
    6. Customer customer=customerService.queryById(id);
    7. customer.setImage(newFileName);
    8. customerService.update(customer);
    9. return customer;
    10. }

    传对象:

    传对象有两种方式

    第一种,使用@SpringQueryMap注解实现

    客户端feign调用接口

    1. @RequestMapping("/ev/insert")
    2. Evaluation insert(@SpringQueryMap Evaluation evaluation);

    服务提供端

    1. @PostMapping("save")
    2. public Object save(@RequestBody Admin admin){
    3. boolean result = false;
    4. //判断是添加还是编辑
    5. if(admin.getId()!=null){
    6. //编辑
    7. // System.out.println("编辑管理员信息");
    8. result = adminService.update(admin)>0;
    9. } else {
    10. //添加
    11. admin.setRegDate(new Date());
    12. // System.out.println("添加管理员信息"+admin);
    13. result = adminService.insert(admin).getId() != null;
    14. }
    15. return result;
    16. }

    重点:多个参数+对象的传值


    在进行多个参数+对象传值时,使用@RequestParam来传递普通参数,使用@SpringQueryMap来传递对象

    注:
    本人亲测踩坑
    使用@RequestParam+@RequestBody的时候,出现问题
    @RequestBody要求前端页面返回json格式,否则会报:不支持Content-Type:application/json的错误

    客户端feign调用接口
     

    1. @RequestMapping(value = "/admin/queryAll", method = RequestMethod.POST)
    2. String queryAll(@RequestParam("page") Integer page,
    3. @RequestParam("limit") Integer limit,
    4. @SpringQueryMap AdminQuery admin);

    服务提供端

    1. @PostMapping("queryAll")
    2. public Object queryAll(Integer page, Integer limit,AdminQuery admin) {
    3. CommonResult result = new CommonResult<>();
    4. IPage ipage = adminService.queryAllByLimit(page,limit,admin);
    5. result.setCode(0);
    6. result.setCount(ipage.getTotal());
    7. result.setData(ipage.getRecords());
    8. return result;
    9. }

  • 相关阅读:
    数据结构——哈希
    Codeforces Round #684 (Div. 1)
    Rowset Class
    机器人精确移动包
    ZOOM 2023校招 第3题(C++并查集)
    DNS的服务与部署(2)
    MONGODB 的基础 NOSQL注入基础
    【前端精进之路】JS篇:第7期 闭包
    JUC 并发编程学习笔记(总)
    Windows、Mac系统常用的SSH工具软件整理汇总
  • 原文地址:https://blog.csdn.net/weixin_67601403/article/details/133375756