最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

Feign使用MultipartFile

维修 admin 502浏览 0评论

Feign使用MultipartFile

Feign的方法参数使用MultipartFile

Feign的方法参数使用MultipartFile,不能直接用,需要做一些配置。

依赖包:

<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-cloud-starter-openfeign。

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

服务提供者

MultipartFile 使用 @RequestPart 修饰。

还需要添加一个消费者类型:consumes = MediaType.MULTIPART_FORM_DATA_VALUE

@RestController
@RequestMapping("/xx")
public class FileController {@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public Response uploadFile(@RequestPart("file") MultipartFile multipartFile, @RequestParam("name") String name) {//....}
}

配置Feign的Encoder

@Configuration
public class FeignMultipartConfig {@Autowiredprivate ObjectFactory<HttpMessageConverters> messageConverters;@Bean@Primary@Scope("prototype")public Encoder feignEncoder() {return new SpringFormEncoder(new SpringEncoder(messageConverters));}
}

FeignClient

FeignClient 指定 configuration = FeignMultipartConfig.class

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

如下所示。

@FeignClient(name = "xx",configuration = FeignMultipartConfig.class)
@RequestMapping("/xx")
public interface FeignService {@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)Response uploadFile(@RequestPart("file") MultipartFile multipartFile, @RequestParam("name") String name);}

服务消费者

MultipartFile用 @RequestPart 修饰即可。

@RestController
@Slf4j
public class FeignController {@Autowiredprivate FeignService feignService;@RequestMapping(value = "/uploadFile")public String fileUpload(@RequestPart("file") MultipartFile file, @RequestParam("name") String name ) {log.info("文件上传---fileName---{}",file.getOriginalFilename());return feignService.uploadFile(file,name);}}

参考资料:

.html

Feign使用MultipartFile

Feign的方法参数使用MultipartFile

Feign的方法参数使用MultipartFile,不能直接用,需要做一些配置。

依赖包:

<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-cloud-starter-openfeign。

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

服务提供者

MultipartFile 使用 @RequestPart 修饰。

还需要添加一个消费者类型:consumes = MediaType.MULTIPART_FORM_DATA_VALUE

@RestController
@RequestMapping("/xx")
public class FileController {@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public Response uploadFile(@RequestPart("file") MultipartFile multipartFile, @RequestParam("name") String name) {//....}
}

配置Feign的Encoder

@Configuration
public class FeignMultipartConfig {@Autowiredprivate ObjectFactory<HttpMessageConverters> messageConverters;@Bean@Primary@Scope("prototype")public Encoder feignEncoder() {return new SpringFormEncoder(new SpringEncoder(messageConverters));}
}

FeignClient

FeignClient 指定 configuration = FeignMultipartConfig.class

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

如下所示。

@FeignClient(name = "xx",configuration = FeignMultipartConfig.class)
@RequestMapping("/xx")
public interface FeignService {@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)Response uploadFile(@RequestPart("file") MultipartFile multipartFile, @RequestParam("name") String name);}

服务消费者

MultipartFile用 @RequestPart 修饰即可。

@RestController
@Slf4j
public class FeignController {@Autowiredprivate FeignService feignService;@RequestMapping(value = "/uploadFile")public String fileUpload(@RequestPart("file") MultipartFile file, @RequestParam("name") String name ) {log.info("文件上传---fileName---{}",file.getOriginalFilename());return feignService.uploadFile(file,name);}}

参考资料:

.html

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论