springcloud中feign使用的抗

  1、feign不支持@GetMapping 和 @PostMapping 注解,只能使用 @RequestMapping 并且 method = RequestMethod.GET必须指定get或post

// 正确写法
@RequestMapping(value = "/user/getList", method = RequestMethod.GET)
List<User> userList();

   2、当feign使用get方式请求时,@PathVariable注解@RequestParam必须设置value属性(@PathVariable或@RequestParam 注解在MVC中不设置value 默认使用当前变量的,但feign中必须设置)

// 正确写法1
@RequestMapping(value = "/user/getList", method = RequestMethod.GET)
List<User> userList(@RequestParam(value="gradeId") Integer gradeId);
// 正确写法2
@RequestMapping(value = "/user/getList", method = RequestMethod.GET)
List<User> userList(@PathVariable(value="gradeId") Integer gradeId);

  3、feign接口如果参数为复杂参数,必须使用post方式

// 正确写法1
@RequestMapping(value = "/user/getList", method = RequestMethod.Post)
List<User> userList(User user);
// 正确写法2(推荐)
@RequestMapping(value = "/user/getList", method = RequestMethod.Post)
List<User> userList(@RequestBody User user);

  

猜你喜欢

转载自www.cnblogs.com/tianchao/p/12462512.html