Springcloud中Feign传递参数(总结) |
您所在的位置:网站首页 › 3ag43参数 › Springcloud中Feign传递参数(总结) |
前言
最近在学习springcloud,在进行springboot拆分成springcloud项目的时候,我使用feign来进行微服务的调用,遇到了一些坑,特此总结,谨记 Feign传递参数 传递单个参数:单个参数的传值有两种方式,第一种 使用@RequestParam/@PathVariable进行传值 客户端feign调用接口(@RequestParam) @RequestMapping("/ct/selectOne") Customer selectOne(@RequestParam("id") Integer id);服务提供端 @RequestMapping("selectOne") public Customer selectOne(Integer id) { return this.customerService.queryById(id); }客户端feign调用接口(@PathVariable) @GetMapping("/admin/selectOne/{id}") String selectOne(@PathVariable("id") Integer id);服务提供端 @RequestMapping("selectOne/{id}") @HystrixCommand(fallbackMethod = "HystrixqueryById") public Admin selectOne(@PathVariable("id") Integer id) { Admin bean = adminService.queryById(id); if(bean == null){ throw new RuntimeException("id:"+id+"没有找到该id的用户"); } return bean; }注意: 1、在使用@RequestParam/@PathVariable进行传值时,一定要注意,需要绑定参数,如@RequestParam(“id”)绑定id,不然会报错 2、@PathVariable是获取url上数据的,@RequestParam获取请求参数的(包括post表单提交) 传递多个参数:多个参数的传值可以使用多个@RequestParam来进行传参 客户端feign调用接口 @RequestMapping("/ct/upload") Customer upload(@RequestParam("newFileName") String newFileName, @RequestParam("id") int id);服务提供端 @RequestMapping("upload") public Customer upload(String newFileName,int id) throws IOException { System.out.println("进入提供者-图片上传"); //设置图片上传路径,是目标文件夹的路径 // 保存到数据库 Customer customer=customerService.queryById(id); customer.setImage(newFileName); customerService.update(customer); return customer; } 传对象:传对象有两种方式 第一种,使用@SpringQueryMap注解实现 客户端feign调用接口 @RequestMapping("/ev/insert") Evaluation insert(@SpringQueryMap Evaluation evaluation);服务提供端 @RequestMapping("insert") public Evaluation insert(Evaluation evaluation) { return evaluationService.insert(evaluation); }第二种,使用@RequestBody传递参数 客户端feign调用接口 @RequestMapping(value = "/admin/save", method = RequestMethod.POST) Object save(@RequestBody Admin admin);服务提供端 @PostMapping("save") public Object save(@RequestBody Admin admin){ boolean result = false; //判断是添加还是编辑 if(admin.getId()!=null){ //编辑 // System.out.println("编辑管理员信息"); result = adminService.update(admin)>0; } else { //添加 admin.setRegDate(new Date()); // System.out.println("添加管理员信息"+admin); result = adminService.insert(admin).getId() != null; } return result; } 重点:多个参数+对象的传值在进行多个参数+对象传值时,使用@RequestParam来传递普通参数,使用@SpringQueryMap来传递对象 注: 本人亲测踩坑 使用@RequestParam+@RequestBody的时候,出现问题 @RequestBody要求前端页面返回json格式,否则会报:不支持Content-Type:application/json的错误 客户端feign调用接口 @RequestMapping(value = "/admin/queryAll", method = RequestMethod.POST) String queryAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @SpringQueryMap AdminQuery admin);服务提供端 @PostMapping("queryAll") public Object queryAll(Integer page, Integer limit,AdminQuery admin) { CommonResult result = new CommonResult(); IPage ipage = adminService.queryAllByLimit(page,limit,admin); result.setCode(0); result.setCount(ipage.getTotal()); result.setData(ipage.getRecords()); return result; }总结: 使用feign来调用服务时,传参会遇到很多坑,尤其时复杂参数的传值,这个时候,就要考验人品了 所以说,平时要多攒攒人品总没有错 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |