使用Feign客户端组件来调用微服务,经常出现参数传不过去变成null的问题,

 

1. 当参数比较复杂时,feign即使声明为get请求也会强行使用post请求。

 

2. 不支持@GetMapping类似注解声明请求,需使用@RequestMapping(value ="url",method = RequestMethod.GET)

 

3. 使用@RequestParam注解时必须要在后面加上参数名。

 

4.传递复杂参数对象需要用Post,另外需要注意,Feign不支持使用GetMapping 和PostMapping

@RequestMapping(value="student/save",method=RequestMethod.POST)

5.在传递的过程中,复杂对象使用@RequestBody进行注解,同时接收端也需要使用@RequestBody这个注解。

消费端使用了@RequestBody而服务端没有接收@RequestBody,这时参数会接收不完整。


//消费端

@RequestMapping(value="student/save",method=RequestMethod.POST)
public Student save(@RequestBody Student student);


//服务端

@PostMapping("save")

 public Student save(@RequestBody Student student) {
    System.out.println(student);
   return StudentService.save(student);
}

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐