通过feign上传文件遇到的问题。

实例代码:

client端引入的依赖包:

    		<dependency>
			<groupId>io.github.openfeign.form</groupId>
			<artifactId>feign-form</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign.form</groupId>
			<artifactId>feign-form-spring</artifactId>
			<version>3.4.1</version>
		</dependency>
复制代码

client端的外部接口:

    @PostMapping(value = "/test")
    BaseResult<String> addDriverFile(TestDto dto, MultipartFile file) {
        return testClient.insertTestInfo(file, dto);
    }
复制代码

client端的feign接口

    //这里的BasicTestDto不加RequestParam("dto")启动会报错,RequestParam.value() was empty on parameter 0
    //并且两个参数只有一个参数是RequestBody,好像RequestPart和RequestBody不能一起发送的吧? 会报这个错误 Method has too many Body parameters。
    
    @PostMapping(value = "/server/test", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    BaseResult<String> insertTestInfo(@RequestPart("file") MultipartFile file, @RequestParam("dto") BasicTestDto dto) throws Exception;
复制代码

server端的外部接口

    @PostMapping(value = "/server/test")
    public BaseResult<String> zipTestUpload(MultipartFile file, HttpServletRequest request) {
        BasicTestDto dto = JSONObject.parseObject(request.getParameter("dto"),BasicTestDto.class);
        return upgradeService.insertTestInfo(dto, file);
    }
复制代码

遇到的问题

  • 可以看到我server端参数用了HttpServletRequest, 之前两个参数是 TestDto dto, MultipartFile file
  • 通过访问client端的外部接口,两个参数TestDto MultipartFile 都获取正常都可以拿到,但通过feign转发之后。 dto的格式变成了_HOZAR2G3[C~}EFZT]28@87.png , {"name":1,"version":1,"factory":1,"supportDeviceType":1},这一段是被toString之后转发过来的。服务端参数dto是接收不到的,
  • 这里贴上之前的server端的代码:
        @PostMapping(value = "/server/test")
        public BaseResult<String> zipTestUpload(@RequestParam MultipartFile file, TestDto dto) {   
        return upgradeService.insertTestInfo(dto, file);
    }
复制代码

** 但我不知道怎么解决这个问题, 只能通过HttpServletRequest去获取了。 如果还有别的解决方法,请大佬们告知**

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享