springcloud~feign POST form-url-encoded data


起因

對於微服務之后發請求,目前使用feign是比較多的,對外部服務也是同樣支持的,有時間我們會有這樣的情況,post請求時,不是使用的json raw的方式,而是使用了application/x-www-form-urlencoded這種方式,對於feign來說,這種方法的post默認是不被支持的,我們需要對feign進行一個擴展。

參考

https://stackoverflow.com/questions/61901362/feignclient-create-post-with-application-x-www-form-urlencoded-body
一般,一個POST的請求是這樣的,它采用application/x-www-form-urlencoded的方式進行提交

curl -X POST \
  https://auth.beyondtime-stage.io/auth/realms/master/protocol/openid-connect/token \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d 'username=admin&password=pass123&client_id=admin-cli&grant_type=password'

解決方案

添加編碼轉換器

    /**
     * 轉換器.
     */
   @Component
    public class FeignConfiguration {
        @Bean
        Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
            return new SpringFormEncoder(new SpringEncoder(converters));
        }
    }

feigenClient的post方式

@FeignClient(name = "keycloak", url = "http://192.168.4.26:8080/auth", configuration = FeignConfiguration .class)
public interface KcUserClient {

    @RequestMapping(value = "/realms/demo/protocol/openid-connect/token",
            method = RequestMethod.POST,
            consumes = "application/x-www-form-urlencoded")
    KeycloakAccessToken login(@RequestBody AuthTokenRequest authTokenRequest);
}

調用

AuthTokenRequest authTokenRequest = new AuthTokenRequest();
authTokenRequest.setClient_id("sms");
authTokenRequest.setGrant_type("password");
authTokenRequest.setPassword("123456");
authTokenRequest.setUsername("test");
authTokenRequest.setClient_secret("877e6236-2326-4837-bdaa-94ec61a95526");
var result=kcUserClient.login(authTokenRequest);

結果的響應
1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM