Giter Site home page Giter Site logo

maciejwalkowiak / spring-boot-http-clients Goto Github PK

View Code? Open in Web Editor NEW
92.0 3.0 9.0 108 KB

Spring Boot HTTP Clients provides zero-boilerplate auto-configuration for WebClient and Spring 6 HTTP Interface based HTTP clients in a Spring Boot application.

License: MIT License

Java 100.00%
spring spring-boot spring-framework webclient httpclient

spring-boot-http-clients's Introduction

Spring Boot HTTP Clients

Spring Boot HTTP Clients provides zero-boilerplate auto-configuration for WebClient and Spring 6 HTTP Interface based HTTP clients in a Spring Boot application.

Note The project is in the early stage, so expect breaking changes.

Spring 6 introduced a new way to define HTTP clients using interfaces - HTTP Interfaces (introduction video: ๐Ÿš€ New in Spring Framework 6: HTTP Interfaces).

Right now, setting up an HTTP client requires a bit of boilerplate code:

  1. Create a property for the base url.
  2. Create a WebClient bean which uses this property
  3. Create an HttpServiceProxyFactory.

This project aims to simplify this process and provide ease of creating HTTP clients similar to Spring Cloud OpenFeign.

Note It's very likely that Spring Boot will eventually implement its own way to autoconfigure HTTP clients, and this project will become deprecated. Look for updates on this topic here: #31337

๐Ÿค” How to install

Add the dependency to spring-boot-http-clients:

<dependency>
    <groupId>com.maciejwalkowiak.spring</groupId>
    <artifactId>spring-boot-http-clients</artifactId>
    <version>0.1.1</version>
</dependency>

โœจ How to use

  1. Define HTTP clients in application.yml or application.properties under the prefix http.clients:
http.clients:
  todo-client:
    url: https://jsonplaceholder.typicode.com/todos
  user-client:
    url: https://jsonplaceholder.typicode.com/users

The client names (in the above example, todo-client and user-client) are just strings - use anything that makes sense - they are going to be used to construct the WebClient bean name.

The above code gets processed by WebClientsAutoConfiguration, which creates a bean of type WebClient with the name <client-name>.WebClient.

  1. Define an HTTP client with Spring 6 HTTP Interface, annotate it with @HttpClient, and set the client name as the parameter:
@HttpClient("todo-client")
public interface TodoClient {
    @GetExchange
    List<Todo> get();
}
  1. That's it! Now you can inject TodoClient anywhere in your code ๐Ÿ™ƒ

๐Ÿ› ๏ธ Advanced usage

Autoconfigured WebClient instances are the result of calling Spring Boot autoconfigured WebClient.Builder#build, which allows defining custom WebClientCustomizer that get applied on the WebClient beans.

If for any reason you cannot rely on the autoconfigured WebClient, but you still want to use autoconfigured HTTP Interface based HTTP client, make sure to create a bean with name <client-name>.WebClient, which will be picked up to create an HTTP client.

Customizing Headers

Default headers can be set in application.yml or application.properties under the prefix http.clients.<client-name>.headers key.

http.clients:
  todo-client:
    url: https://jsonplaceholder.typicode.com/todos
    headers:
      X-My-Header: my-value-x
      Y-My-Header: ${DYNAMIC_VALUE:default-value}

Customizing Cookies

Default cookies can be set in application.yml or application.properties under the prefix http.clients.<client-name>.cookies key.

http.clients:
  todo-client:
    url: https://jsonplaceholder.typicode.com/todos
    cookies:
      someCookie: cookie-value
      someDynamicCookie: ${DYNAMIC_VALUE:default-value}

Adding Filters

Filter functions can be defined in application.yml or application.properties under the prefix http.clients.<client-name>.filters key.

http.clients:
  todo-client:
    url: https://jsonplaceholder.typicode.com/todos
    filters:
      - filterFunction
      - filterFunction2

๐Ÿ‘ฅ Contributing

If you found a bug or a missing feature - you're very welcome to submit an issue and a pull request with a fix. Note, that this library is intended only to glue things together and not to compensate on the missing features in Spring's support for HTTP interfaces.

Sounds good? Consider โค๏ธ Sponsoring the project! Thank you!

spring-boot-http-clients's People

Contributors

babltiga avatar cookabc avatar maciejwalkowiak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spring-boot-http-clients's Issues

Add @Service for HttpClient annotation

Now if you refer HTTP Interface as bean in IntelliJ IDEA, and IDEA will report Could not autowire. No beans of 'HttpBinService' type found. .

Screenshot 2023-04-05 at 5 41 02 PM

spring-boot-http-clients relies on Spring, and @Service could be added in HttpClient annotation, and make HTTP interface as a bean.

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE})
@Documented
@Service
public @interface HttpClient {

    /**
     * Name of the {@link WebClient} bean to use with http client
     * @return bean name
     */
    String value();
}

Cannot configure headers in application.yml

Hi! Does the released version contain the same source code as the one in github repository? I can see on my machine, that it doesn't contain code for configuring headers in application.yml

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.