Feign for file upload form call +

Feigin default does not support file upload and submit the form, you need to do some configuration to support.

1, feign dependence

 

 The red is a form of support must jar.

 

2, feign interface class:

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);
}

Wherein the request to support the form, the need for this separate configuration 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. Service implementation class

@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
}

 

Guess you like

Origin www.cnblogs.com/yangzhilong/p/11714620.html