欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于feign傳參MultipartFile問題解決

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

feign傳參MultipartFile問題

首先,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,否則還是無效的

新建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的一些問題

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中Map集合的常用方法(非常詳細(xì)!)

    Java中Map集合的常用方法(非常詳細(xì)!)

    Java中的Map是一種鍵值對(duì)存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu),它提供了快速查找和訪問數(shù)據(jù)的能力,下面這篇文章主要給大家介紹了關(guān)于Java中Map集合的常用方法,需要的朋友可以參考下
    2024-01-01
  • 關(guān)于SpringMVC在Controller層方法的參數(shù)解析詳解

    關(guān)于SpringMVC在Controller層方法的參數(shù)解析詳解

    在SpringMVC中,控制器Controller負(fù)責(zé)處理由DispatcherServlet分發(fā)的請(qǐng)求,下面這篇文章主要給大家介紹了關(guān)于SpringMVC在Controller層方法的參數(shù)解析的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • java中String StringBuffer和StringBuilder的區(qū)別詳解

    java中String StringBuffer和StringBuilder的區(qū)別詳解

    大家好,本篇文章主要講的是java中String StringBuffer和StringBuilder的區(qū)別詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Java異常的處理機(jī)制

    Java異常的處理機(jī)制

    這篇文章主要介紹了Java異常的處理機(jī)制,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Java中int和Integer的區(qū)別

    Java中int和Integer的區(qū)別

    這篇文章主要介紹的是?Java中int和Integer的區(qū)別,Java?是一種強(qiáng)數(shù)據(jù)類型的語言,因此所有的屬性必須有一個(gè)數(shù)據(jù)類型,下面文章基于Java詳細(xì)int和Integer有何區(qū)別,需要的朋友可以參考一下
    2021-11-11
  • Spring Boot文件上傳原理與實(shí)現(xiàn)詳解

    Spring Boot文件上傳原理與實(shí)現(xiàn)詳解

    這篇文章主要介紹了Spring Boot 文件上傳原理與實(shí)現(xiàn)詳解,前端文件上傳是面向多用戶的,多用戶之間可能存在上傳同一個(gè)名稱、類型的文件;為了避免文件沖突導(dǎo)致的覆蓋問題這些應(yīng)該在后臺(tái)進(jìn)行解決,需要的朋友可以參考下
    2024-01-01
  • Hibernate基于ThreadLocal管理Session過程解析

    Hibernate基于ThreadLocal管理Session過程解析

    這篇文章主要介紹了Hibernate基于ThreadLocal管理Session過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • springboot注入servlet的方法

    springboot注入servlet的方法

    本篇文章主要介紹了springboot注入servlet的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • springboot項(xiàng)目docker分層構(gòu)建的配置方式

    springboot項(xiàng)目docker分層構(gòu)建的配置方式

    在使用dockerfile構(gòu)建springboot項(xiàng)目時(shí),速度較慢,用時(shí)比較長(zhǎng),為了加快構(gòu)建docker鏡像的速度,采用分層構(gòu)建的方式,這篇文章主要介紹了springboot項(xiàng)目docker分層構(gòu)建,需要的朋友可以參考下
    2024-03-03
  • java獲取本地文件的多種方式實(shí)現(xiàn)與比較

    java獲取本地文件的多種方式實(shí)現(xiàn)與比較

    這篇文章主要為大家詳細(xì)介紹了java獲取本地文件的多種方式實(shí)現(xiàn)與結(jié)果比較,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11

最新評(píng)論