最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于feign傳參MultipartFile問(wèn)題解決

 更新時(shí)間:2022年03月04日 11:28:47   作者:qq_42151769  
這篇文章主要介紹了基于feign傳參MultipartFile問(wèn)題解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

feign傳參MultipartFile問(wèn)題

首先,feign服務(wù)之間的調(diào)用,傳參默認(rèn)的格式為:ContentType=application/x-www-form-urlencoded

以表單的形式傳參的,而文件流的傳參,需要form-data的ContentType,否則會(huì)報(bào)錯(cuò)的

首先引入依賴

 <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.8.0</version>
        </dependency>
 
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.8.0</version>
        </dependency>

注意spring boot版本是2.x以上的,上面兩個(gè)依賴的版本不低于3.5.0,否則還是無(wú)效的

新建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.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 
 
/**
 * @author :半卷流年
 * @description : 解決feign傳遞流數(shù)據(jù)的異常
 * @createTime :2020/6/14
 */
@Configuration
public class FeignSupportConfig { 
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
 
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    } 
}

在feign接口中配置

package com.wm.blog_admin.feign; 
import com.wm.blog_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調(diào)用  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<TFileDO> get(@PathVariable("id") Long id); 
 
    /**
     * id批量查詢圖片信息
     * @param idList
     * @return
     */
    @PostMapping("/web/picture/getByIdList")
    Result<List<TFile>> getByIdList(@RequestBody List<Long> idList); 
 
    /**
     * 文件上傳
     * @param file
     * @return
     */
    @PostMapping(value = "/web/picture/uploadFile",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     Result<String> uploadFile(@RequestPart("file") MultipartFile file);
 
}

注意加上這個(gè),表示傳參格式:

就可以傳參了的

Feign傳輸 MultipartFile的一些問(wèn)題

File轉(zhuǎn)MultipartFile

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
public static MultipartFile getMultipartFile(String fileName, File file) throws IOException {
? ? return new MockMultipartFile(fileName, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));
}

報(bào)錯(cuò) Current request is not a multipart request、Content type ‘’ not supported

@PostMapping設(shè)置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE

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

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResultBody upload(@RequestPart(value = "file") MultipartFile file);

報(bào)錯(cuò) Required request part ‘file’ is not present

configuration

@Configuration
public class UploadFeignConfig {
? ? @Bean
? ? public Encoder multipartFormEncoder() {
? ? ? ? return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public 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);
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺析Java方法傳值和傳引用問(wèn)題

    淺析Java方法傳值和傳引用問(wèn)題

    這篇文章主要是對(duì)Java方法傳值和傳引用問(wèn)題進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-12-12
  • java正則表達(dá)式使用示例

    java正則表達(dá)式使用示例

    這篇文章主要介紹了java正則表達(dá)式使用示例,實(shí)現(xiàn)拆分字符串、替換字符串、判斷字符串是否與制定模式匹配等功能,需要的朋友可以參考下
    2014-03-03
  • JUC系列學(xué)習(xí)工具類CountDownLatch詳解

    JUC系列學(xué)習(xí)工具類CountDownLatch詳解

    這篇文章主要介紹了JUC系列學(xué)習(xí)工具類CountDownLatch詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可任意參考一下
    2022-08-08
  • java中@Configuration使用場(chǎng)景

    java中@Configuration使用場(chǎng)景

    本文主要介紹了java中@Configuration使用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • spring如何通過(guò)FactoryBean配置Bean

    spring如何通過(guò)FactoryBean配置Bean

    這篇文章主要介紹了spring如何通過(guò)FactoryBean配置Bean,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • java?poi?讀取單元格null或者空字符串方式

    java?poi?讀取單元格null或者空字符串方式

    這篇文章主要介紹了java?poi?讀取單元格null或者空字符串方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • springboot使用AOP+反射實(shí)現(xiàn)Excel數(shù)據(jù)的讀取

    springboot使用AOP+反射實(shí)現(xiàn)Excel數(shù)據(jù)的讀取

    本文主要介紹了springboot使用AOP+反射實(shí)現(xiàn)Excel數(shù)據(jù)的讀取,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java開(kāi)發(fā)JUC交換器Exchanger使用詳解

    Java開(kāi)發(fā)JUC交換器Exchanger使用詳解

    這篇文章主要為大家介紹了Java開(kāi)發(fā)JUC交換器Exchanger使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn)

    SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java中逆序遍歷List集合的實(shí)現(xiàn)

    Java中逆序遍歷List集合的實(shí)現(xiàn)

    本文主要介紹了Java中逆序遍歷List集合的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01

最新評(píng)論

灵武市| 乐平市| 荔波县| 调兵山市| 界首市| 固原市| 连南| 许昌县| 长寿区| 全州县| 资中县| 金阳县| 黄梅县| 广德县| 休宁县| 望江县| 桐庐县| 河西区| 新安县| 闻喜县| 阿瓦提县| 长沙市| 陵川县| 五华县| 农安县| 洛扎县| 绵竹市| 刚察县| 安平县| 安庆市| 子洲县| 哈巴河县| 禹州市| 高安市| 革吉县| 陕西省| 乐至县| 葵青区| 江安县| 四川省| 武城县|