起因
對於微服務之后發請求,目前使用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);
結果的響應