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

com.c4_soft.springaddons.rest.WebClientHttpExchangeProxyFactoryBean Maven / Gradle / Ivy

package com.c4_soft.springaddons.rest;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import lombok.NoArgsConstructor;

/**
 * Bean factory using {@link HttpServiceProxyFactory} to generate an instance of a
 * {@link HttpExchange @HttpExchange} interface
 * 
 * @param  {@link HttpExchange @HttpExchange} interface to implement
 * @see RestClientHttpExchangeProxyFactoryBean RestClientHttpExchangeProxyFactoryBean for an
 *      equivalent accepting only RestClient
 * @see HttpExchangeProxyFactoryBean HttpExchangeProxyFactoryBean for an equivalent accepting both
 *      RestClient an WebClient
 * @author Jérôme Wacongne <ch4mp@c4-soft.com>
 */
@NoArgsConstructor
public class WebClientHttpExchangeProxyFactoryBean implements FactoryBean {
  private Class httpExchangeClass;
  private HttpExchangeAdapter adapter;

  public WebClientHttpExchangeProxyFactoryBean(Class httpExchangeClass, WebClient client) {
    this.httpExchangeClass = httpExchangeClass;
    this.setClient(client);
  }

  @Override
  @Nullable
  public T getObject() throws Exception {
    if (adapter == null || getObjectType() == null) {
      throw new RestMisconfigurationException(
          "Both of a WebClient and the @HttpExchange interface to implement must be configured on the HttpExchangeProxyFactoryBean before calling the getObject() method.");
    }
    return HttpServiceProxyFactory.builderFor(adapter).build().createClient(getObjectType());
  }

  @Override
  public Class getObjectType() {
    return httpExchangeClass;
  }

  public WebClientHttpExchangeProxyFactoryBean setHttpExchangeClass(Class httpExchangeClass) {
    this.httpExchangeClass = httpExchangeClass;
    return this;
  }

  public WebClientHttpExchangeProxyFactoryBean setClient(WebClient client) {
    this.adapter = WebClientAdapter.create(client);
    return this;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy