网站服务器租赁,深圳网站设计教程,注册个免费网站,搭建服务平台目录代码示例文件服务接口调用方注意点#xff1a;2021.3.16更新#xff1a;发新的坑坑点#xff1a;原因分析部分异常及解决方案异常一#xff1a;[Method has too many Body parameters](https://blog.csdn.net/haishiyizhenfeng/article/details/80607003)异常二#x…
目录代码示例文件服务接口调用方注意点2021.3.16更新发新的坑坑点原因分析部分异常及解决方案异常一[Method has too many Body parameters](https://blog.csdn.net/haishiyizhenfeng/article/details/80607003)异常二[FeignClient注入找不到的异常](https://blog.csdn.net/qq_28165595/article/details/102328066)异常三[feign.FeignException$MethodNotAllowed: status 405](https://blog.csdn.net/qq_43371556/article/details/100548389)其他问题代码示例 文件服务接口 /*** 用于上传文传* UploadFile包含fileName、fileDesc* 不加RequestParam等同于RequestParam(required false)* * param file 文件* param entity 文件的描述* return*/PostMapping(/add)public ResponseObject add(RequestPart(value file) MultipartFile file,UploadFile entity,) {ResponseObject res new Response();FileHandlerResult handlerResult service.saveFile(file);return res;}调用方
FeignClient(value file-server)
public interface FileService {PostMapping(value /static/add, consumes MediaType.MULTIPART_FORM_DATA_VALUE)ResponseLinkedHashMapObject, Object saveFile(RequestPart(value file) MultipartFile file,RequestParam(value fileName)String fileName,RequestParam(value fileDesc)String fileDesc);
}注意点
参数是文件类型的要用RequestPart注解调用方需要设置ContentType为multipart/form-data
PostMapping(value xxx, consumes MediaType.MULTIPART_FORM_DATA_VALUE)调用方的文件参数名必须与服务接口的文件参数名相同示例中文件参数都为RequestPart(value file) MultipartFile file参数名不同会导致服务接口接收到的文件为NULL。
2021.3.16更新发新的坑
坑点
调用方在接受MultipartFile参数并传给服务方时必须保持MultipartFile参数的参数名与服务方一致(即multipartFile.getName()必须与服务方的RequestPart(value xxx) MultipartFile file的value值相同)否则服务方会找不到参数
最近在补充原本的分布式项目时发现在调用发Controller层接受文件参数后使用OpenFeign调用文件上传服务一直报错没有接受到参数file
Required request part file is not present报错时调用方Controller代码 /*** RequestParam(name)MultipartFile[] file* 中的name, 即参数名必须与调用方接口的参数名相同* 否则服务方会报错Required request part file is not present*/PostMapping(/upload)public ResponseObject upload(RequestParam(image)MultipartFile image){ResponseObject res new Response();ListObject data new LinkedList();//调用服务方的接口ResponseLinkedHashMapObject, Object response fileService.saveFile(image);if(response.getCode()400){//保存失败res.fail(response.getDesc());return res;}data.add(response);res.success(data);return res;}服务方代码 PostMapping(/add)public ResponseObject add(RequestPart(value file) MultipartFile file,UploadFile entity,) {ResponseObject res new Response();FileHandlerResult handlerResult service.saveFile(file);return res;}此时服务方接受不到调用方传的文件参数。
原因分析
在调用方的Controller中参数为RequestParam(image)MultipartFile image 输出image的文件名可以发现输出为image与RequestParam(image)的参数名一致
//输出结果image
System.out.println(image.getName());而服务方接口需要的文件名 RequestPart(value file) MultipartFile file即参数名字需要为file而openfeign传给服务方的文件参数名为image因此参数传递失败。
由此可知调用方在接受MultipartFile参数并传给服务方时必须保持MultipartFile参数的参数名与服务方一致否则会找不到参数
部分异常及解决方案
异常一Method has too many Body parameters
GetMapping(value/test)
Model test(String arg1, String arg1); 异常原因
当使用Feign时如果发送的是get请求那么需要在请求的所有参数前加上RequestParam注解修饰RequestParam是用来修饰参数不能用来修饰整个对象。
注意使用RequestParam修饰参数请求默认的Content-Type 为 application/x-www-form-urlencoded
RequestBody用来修饰对象
Feign中可以有多个RequestParam但只能有不超过一个RequestBody既有RequestBody也有RequestParam那么参数就要放在请求的url中RequestBody修饰的就要放在提交对象中。
示例
public int save(RequestBody Person p, RequestParam(userId) String userId);注意使用RequestBody修饰参数请求的默认Content-Type 为 application/json 或 application/xml
异常二FeignClient注入找不到的异常
异常原因 可能为启动类没有标注EnableFeignClients Springcloud中的服务间调用是通过Feign进行调用的在调用方服务中我们需要定义一些带有FeignClient注解的接口类。并且在启动类上加上EnableFeignClients注解。程序启动的时候会检查是否有EnableFeignClients注解如果有该注解则开启包扫描扫描带有FeignClient注解的接口。 异常三feign.FeignException$MethodNotAllowed: status 405
异常原因 调用方的方法参数没有标注RequestParam或参数名与服务接口的参数名不相同
其他问题
若是服务接口接受到的文件一直为NULL请检测调用方法的文件参数名是否与服务接口文件参数名Feign部分版本好像不支持传文件的同时传文件的参数Hoxton.SR1没有这个问题