微服务Feign调用报错:Body parameter 0 was null at feign.Util.checkArgument
报错详细内容如下:
java.lang.IllegalArgumentException: Body parameter 0 was null
at feign.Util.checkArgument(Util.java:96)
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:388)
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:232)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:84)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
at com.sun.proxy.$Proxy112.getVideoLoginParamByDeviceId(Unknown Source)
at com.test.controller.DeviceController.getToken(DeviceController.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at ...
报错原因分析:
微服务使用feign进行跨服务调用时导致,报错原因分析就是少参数了。A服务通过feign调用B服务,B服务需要传一个参数,但是A服务调用时却报错参数是空的!代码如下,A服务实际是传参了,但是系统没有识别到参数
A服务FeignService代码:
@FeignClient("server-b")
public interface BFeignService {
@GetMapping("/bApi/device/getDeviceById")
Map<String,Object> getDeviceById(String deviceId);
}
B服务Controller代码:
@RestController("device")
public interface DeviceController {
@GetMapping("/getDeviceById")
public Map<String, Object> getDeviceById(String deviceId){
Map<String,Object> param = new HashMap<>();
//代码略
return param;
}
}
报错解决:
使用feign跨服务调用时,如果A服务调用B服务的Get请求传单个参数,A服务的FeignService传参时需要加入@RequestParam的注解,如下:
@FeignClient("server-b")
public interface BFeignService {
@GetMapping("/bApi/device/getDeviceById")
Map<String,Object> getDeviceById(@RequestParam("deviceId") String deviceId);
}
这样再请求就可以获取到正常的值了,数据传输也都一切正常了。 另外,正常跨服务Post请求通过Map传输数据时,正常传参就可以,不需要加@RequestParam的注解!