Feigin默认是不支持文件上传和表单提交的,需要做一些配置才能支持。
1、feign依赖
图中红色为form支持必须的jar。
2、feign接口类:
import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import com.longge.common.GlobalResponse; import com.longge.dto.EmailDto; /** * @author: yangzhilong * @description: * post with MULTIPART_FORM_DATA_VALUE * you client feign config: * ------------------------------------------------------------------- * import org.springframework.cloud.openfeign.FeignClient; import org.springframework.context.annotation.Bean; import feign.codec.Encoder; import feign.form.spring.SpringFormEncoder; @FeignClient(value = "notification-service", configuration = NotificationServiceFeign.FeignSimpleEncoderConfig.class) public interface NotificationServiceFeign extends NotificationMultipartService { class FeignSimpleEncoderConfig { @Bean public Encoder encoder(){ return new SpringFormEncoder(); } } } * --------------------------------------------------------------------- **/ @RequestMapping(value = "/v1/api") public interface NotificationMultipartService { /** * common email channel ,use to send common email * * <p>content Support:txt,html,attachmentfile;</p> * <p>attachfile not required</p> * * @param attachfile attach file * @param emailDto email sender param * @return GlobalResponse<Long> email id */ @RequestMapping(value = "/send-mail", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) GlobalResponse<String> sendMail(@RequestParam(value = "attachfile", required = false) MultipartFile attachfile, EmailDto emailDto); }
其中为了支持 form请求,需要对此feign进行单独的配置:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.context.annotation.Bean; import feign.codec.Encoder; import feign.form.spring.SpringFormEncoder; @FeignClient(value = "notification-service", configuration = NotificationServiceFeign.FeignSimpleEncoderConfig.class) public interface NotificationServiceFeign extends NotificationMultipartService { class FeignSimpleEncoderConfig { @Bean public Encoder encoder(){ return new SpringFormEncoder(); } } }
3、服务实现类
@RequestMapping(value = "/v1/api/send_mail", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE}) public GlobalResponse<String> sendMail(@RequestParam(value = "attachfile", required = false) MultipartFile[] attachfile, EmailDto emailDto) { // TODO }