Java

Feign POST application/x-www-form-urlencoded

HanSeokhyeon 2022. 4. 12. 17:43
반응형

FormConfiguration.java

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


public class FormConfiguration {

    @Bean
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters(new RestTemplate().getMessageConverters())));
    }
}

 

Client.java

@FeignClient(name = "a", url = "localhost", fallbackFactory = ClientImpl.class, configuration = FormConfiguration.class)
public interface Client {

    @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    TokenDto getToken(@RequestBody Map<String, ?> form);
    
}

 

Service.java

private final Client client;

private String getToken() {
        Map<String, String> form = new HashMap<>();
        form.put("username", "testid");
        form.put("password", "testpassword");
        TokenDto token = client.getToken(form);
        return token.getAccessToken();
}

 

반응형