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

FeignMultipartSupportConfig上傳圖片配置方式

 更新時間:2022年03月04日 11:42:12   作者:狼煙的煙  
這篇文章主要介紹了FeignMultipartSupportConfig上傳圖片配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

FeignMultipartSupportConfig上傳圖片配置

在對應的boot項目上關閉全局的上傳圖片的配置

@SpringBootApplication
@EnableCircuitBreaker
@EnableEurekaClient
@EnableFeignClients
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = FeignMultipartSupportConfig.class)})
public class BootstrapApplication {
?
? ?public static void main(String[] args) {
? ? ? SpringApplication.run(BootstrapApplication.class, args);
? ?}
}

在目標feign上面添加

@FeignClient(name = "micro-picture", fallbackFactory = MicroPictureFactory.class, configuration = FeignMultipartSupportConfig.class)
public interface MicroPictureClient {
@RequestMapping(value = { "/picture/common/upload/{commonKey}" }, method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
? ?public String upload(@RequestPart("image") MultipartFile image, @PathVariable("commonKey") Long commonKey);
? ?
}

就可以實現(xiàn)對應的服務做圖片的上傳,針對的圖片微服務就可以實現(xiàn)數(shù)據(jù)的額接收。

對應配置文件的代碼

@Configuration
public class FeignMultipartSupportConfig {
? ?@Bean
? ?@Primary
? ?@Scope("prototype")
? ?public Encoder multipartFormEncoder() {
? ? ? return new FeignSpringFormEncoder();
? ?}
? ?@Bean
? ?public feign.Logger.Level multipartLoggerLevel() {
? ? ? return feign.Logger.Level.FULL;
? ?}
}
package com.zhht.config;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.ContentType;
import feign.form.FormEncoder;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;
public class FeignSpringFormEncoder extends FormEncoder {
? ?
? ?public FeignSpringFormEncoder() {
? ? ? this(new Default());
? ?}
? ?public FeignSpringFormEncoder(Encoder delegate) {
? ? ? super(delegate);
? ? ? MultipartFormContentProcessor processor = (MultipartFormContentProcessor) this
? ? ? ? ? ? .getContentProcessor(ContentType.MULTIPART);
? ? ? processor.addWriter(new SpringSingleMultipartFileWriter());
? ? ? processor.addWriter(new SpringManyMultipartFilesWriter());
? ?}
? ?public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
? ? ? if (bodyType.equals(MultipartFile.class)) {
? ? ? ? ?MultipartFile file = (MultipartFile) object;
? ? ? ? ?if (file != null) {
? ? ? ? ? ? Map<String, Object> data = Collections.singletonMap("image", object);
? ? ? ? ? ? super.encode(data, MAP_STRING_WILDCARD, template);
? ? ? ? ? ? return;
? ? ? ? ?}
? ? ? } else if (bodyType.equals(MultipartFile[].class)) {
? ? ? ? ?MultipartFile[] file = (MultipartFile[]) object;
? ? ? ? ?if (file != null) {
? ? ? ? ? ? Map<String, Object> data = Collections.singletonMap("imgList", object);
? ? ? ? ? ? super.encode(data, MAP_STRING_WILDCARD, template);
? ? ? ? ? ? return;
? ? ? ? ?}
? ? ? }
? ? ? super.encode(object, bodyType, template);
? ?}
}

如何使用Feign上傳圖片

添加依賴,支持SpringEncoder

? ? ? ? <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>

將SpringFormEncoder的默認處理

encoder配置為SpringEncoder

@Configuration
public class FeignMultipartSupportConfig {
? ? @Bean
? ? public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
? ? ? ? return new SpringFormEncoder(new SpringEncoder(messageConverters));
? ? }
}

編寫client

@FeignClient(value = "****",
? ? ? ? fallbackFactory = UploadClientFallbackFactory.class
? ? ? ?, configuration = FeignMultipartSupportConfig.class
)
public interface UploadClient {
? ? /**
? ? ?* 上傳圖片文件
? ? ?*
? ? ?* @param file
? ? ?* @return
? ? ?*/
? ? @PostMapping(value = "/tbk/feedback/upload",
? ? ? ? ? ? produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
? ? ? ? ? ? consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
? ? BaseResponse<String> uploadImage(@RequestPart("file") MultipartFile file);
}

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

相關文章

  • MyBatisPlus深入探究映射匹配的兼容性

    MyBatisPlus深入探究映射匹配的兼容性

    在最近的工作中,碰到一個比較復雜的返回結果,發(fā)現(xiàn)簡單映射已經(jīng)解決不了這個問題了,只好去求助百度,學習mybatis映射匹配應該怎么寫,將學習筆記結合工作碰到的問題寫下本文,供自身查漏補缺,同時已被不時之需
    2022-08-08
  • spring中ApplicationListener的使用小結

    spring中ApplicationListener的使用小結

    ApplicationListener是spring提供的一個監(jiān)聽器,本文主要介紹了spring中ApplicationListener的使用小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • 高并發(fā)系統(tǒng)的限流詳解及實現(xiàn)

    高并發(fā)系統(tǒng)的限流詳解及實現(xiàn)

    這篇文章主要介紹了高并發(fā)系統(tǒng)的限流詳解及實現(xiàn),內容詳細,小編覺得很不錯,這里分享給大家,供需要的朋友參考。隨小編一起看看吧。
    2017-11-11
  • 基于binarywang封裝的微信工具包生成二維碼

    基于binarywang封裝的微信工具包生成二維碼

    這篇文章主要介紹了基于binarywang封裝的微信工具包生成二維碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • 最新評論

    白河县| 宁强县| 观塘区| 河西区| 蒲城县| 独山县| 康保县| 汝城县| 礼泉县| 万安县| 高淳县| 双峰县| 营山县| 宝坻区| 建阳市| 蒲城县| 泾源县| 区。| 顺平县| 六安市| 汕尾市| 惠来县| 商南县| 台江县| 莱州市| 葫芦岛市| 辛集市| 麟游县| 固始县| 铁岭县| 左贡县| 章丘市| 大港区| 灌南县| 府谷县| 新乡县| 布拖县| 大宁县| 南京市| 醴陵市| 八宿县|