All Downloads are FREE. Search and download functionalities are using the official Maven repository.

feign.form.spring.SpringFormEncoder Maven / Gradle / Ivy

There is a newer version: 3.8.0
Show newest version
package feign.form.spring;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.FormEncoder;

/**
 * Adds support for {@link MultipartFile} type to {@link FormEncoder}.
 * 
 * @author Tomasz Juchniewicz 
 * @since 14.09.2016
 */
public class SpringFormEncoder extends FormEncoder {
    
    private final Encoder delegate;

    public SpringFormEncoder () {
        this(new Encoder.Default());
    }

    public SpringFormEncoder(Encoder delegate) {
        this.delegate = delegate;
    }
    
    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        if (!bodyType.equals(MultipartFile.class)) {
            delegate.encode(object, bodyType, template);
            return;
        }
        
        MultipartFile file = (MultipartFile) object;
        Map data = Collections.singletonMap(file.getName(), object);
        new SpringMultipartEncodedDataProcessor().process(data, template);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy