https://stackoverflow.com/questions/61901362/feignclient-create-post-with-application-x-www-form-urlencoded-body

FeignClient create POST with application/x-www-form-urlencoded body

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'
import com.beyondtime.recruitmentservice.seafarer.entity.AuthTokenRequest;
import com.beyondtime.recruitmentservice.seafarer.entity.KeycloakAccessToken;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@FeignClient(name = "beyondtime-io-kc-client", url = "${btio.keycloakUrl}", path = "/", configuration = KeycloakAdminClient.Configuration.class)
public interface KeycloakAdminClient {

    @PostMapping(value="realms/master/protocol/openid-connect/token", consumes = "application/x-www-form-urlencoded")
    KeycloakAccessToken getAuthToken(@RequestBody AuthTokenRequest authTokenRequest);

    class Configuration {
        @Bean
        Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
            return new SpringFormEncoder(new SpringEncoder(converters));
        }
    }
}

Request object:

@Getter
@Setter
public class AuthTokenRequest {
    private String username;

    private String password;

    private String client_id;

    private String grant_type;

}

Response object:

@Getter
@Setter
public class KeycloakAccessToken {
    @JsonProperty("access_token")
    private String accessToken;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐