基于feign传参MultipartFile问题解决

网友投稿 576 2022-08-23


基于feign传参MultipartFile问题解决

目录feign传参MultipartFile问题首先引入依赖新建feign的配置在feign接口中配置Feign传输MultipartFile的一些问题File转MultipartFile

feign传参MultipartFile问题

首先,feign服务之间的调用,传参默认的格式为:ContentType=application/x-www-form-urlencoded

以表单的形式传参的,而文件流的传参,需要form-data的ContentType,否则会报错的

首先引入依赖

io.github.openfeign.form

feign-form

3.8.0

io.github.openfeign.form

feign-form-spring

3.8.0

注意spring boot版本是2.x以上的,上面两个依赖的版本不低于3.5.0,否则还是无效的

新建feign的配置

package com.wm.blog_config.config;

import feign.codec.Encoder;

import feign.form.spring.SpringFormEncoder;

import org.springframework.beans.factory.ObjectFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;

import org.springframework.cloud.openfeign.supporthttp://.SpringEncoder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author :半卷流年

* @description : 解决feign传递流数据的异常

* @createTime :2020/6/14

*/

@Configuration

public class FeignSupportConfig {

@Autowired

private ObjectFactory messageCiYrIlDonverters;

@Bean

public Encoder feignFormEncoder() {

return new SpringFormEncoder(new SpringEncoder(messageConverters));

}

}

在feign接口中配置

package com.wm.blog_admin.feign;

import com.wm.bloiYrIlDg_admin.feign.factory.PictureClientFallbackFactory;

import com.wm.blog_common.constatnt.CommonConstant;

import com.wm.blog_common.domain.TFileDO;

import com.wm.blog_common.entity.TFile;

import com.wm.blog_common.req.TFileQuery;

import com.wm.blog_common.result.Result;

import com.wm.blog_config.config.CustomFeignConfig;

import com.wm.blog_config.config.FeignSupportConfig;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/***

* @ClassName: PictureFeignClient

* @Description: picture feign调用 todo feign使用get有坑啊,是否考虑使用HttpClient替换feign的HttpURLConnection,采用apache的HttpClient

* @Author: wm_yu

* @Create_time: 16:39 2020-3-26

*/

@FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = {CustomFeignConfig.class, FeignSupportConfig.class}, fallbackFactory = PictureClientFallbackFactory.class)

public interface PictureFeignClient {

/**

* id查询图片信息

* @param id

* @return

*/

@GetMapping("/web/picture/{id}")

Result get(@PathVariable("id") Long id);

/**

* id批量查询图片信息

* @param idList

* @return

*/

@PostMapping("/web/picture/getByIdList")

Result> getByIdList(@RequestBody List idList);

/**

* 文件上传

* @param file

* @return

*/

@PostMapping(value = "/web/picture/uploadFile",produces = {MediaType.APPLICATION_jsON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

Result uploadFile(@RequestPart("file") Muhttp://ltipartFile file);

}

注意加上这个,表示传参格式:

就可以传参了的

Feign传输 MultipartFile的一些问题

File转MultipartFile

pom.xml

org.springframework

spring-mock

2.0.8

public static MultipartFile getMultipartFile(String fileName, File file) throws IOException {

return new MockMultipartFile(fileName, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));

}

报错 Current request is not a multipart request、Content type ‘’ not supported

@PostMapping设置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE

使用@RequestPart(),不能使用@RequestParam()

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

ResultBody upload(@RequestPart(value = "file") MultipartFile file);

报错 Required request part ‘file’ is not present

configuration

@Configuration

public class UploadFeignConfig {

@Bean

public Encoder multipartFormEncoder() {

return new SpringFormEncoder(new SpringEncoder(new ObjectFactory() {

@Override

publichttp:// HttpMessageConverters getObject() throws BeansException {

return new HttpMessageConverters(new RestTemplate().getMessageConverters());

}

}));

}

}

FeignClient

@FeignClient(value = FileConstants.FILE_SERVER, configuration= UploadFeignConfig.class)

public interface FileServiceClient extends IFileServiceClient {

@Override

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

ResultBody upload(@RequestPart(value = "file") MultipartFile file);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:数据结构与算法Java版-逆序对问题(java数组逆序输出)
下一篇:排序算法Java版-快速排序算法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~