Code Monkey home page Code Monkey logo

spring-webflux-client's Introduction

Build Status

Reactive-Client

The goal of this library is to ease the use of latest Spring Reactive library Spring-Webflux that can be a bit verbose sometimes.

Comparison

Using Reactive-client:

public interface AccountClient {
    @GetMapping(value = "/accounts/{id}", consumes = APPLICATION_JSON_VALUE)
    Mono<Account> getAccount(@PathVariable("id") Integer id);
}
...
AccountClient accountClient = return ClientBuilder
	.defaults(AccountClient.class, "http://example.com");

Using Spring-webflux:

This is the code you have to write in order to achieve the same result as in the previous example.

public class AccountClient {
    private WebClient client = WebClient.create("http://example.com");
    
    public Mono<Account> getAccount(Integer id) {
        return client.get()
            .url("/accounts/{id}", id)
            .accept(APPLICATION_JSON)
            .exchange(request)
            .then(response -> response.bodyToMono(Account.class));
    }
}

Features

Request headers

Static headers can be added to the request using Spring mapping annotations:

  • @RequestMapping
  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping
@RequestMapping(headers = {"x-api-token=static-token"})

Dynamic headers can be added to the request using a parameter on the method

@GetMapping("/accounts/{id}")
Mono<Account> getAccount(@PathVariable("id") Integer id, @RequestHeader("x-api-token") String token);

Request parameters

Spring annotation @RequestParam configure the request parameters on the request

@GetMapping(value = "/accounts")
Flux<Account> getAccounts(@RequestParam("limit") Integer limit);

You don't need to specify anything in the uri. When the annotation is processed, request parameters will be automatically added to the request path, this will generate this URI: /accounts?limit={limit}

Request body

Request body configuration:

  1. With the Spring annotation @RequestBody :
@PostMapping(value = "/accounts")
Flux<Account> createAccounts(@RequestBody Account newAccount);
  1. Without annotation. Be careful, you can only have one parameter without annation on each method.
@PostMapping(value = "/accounts")
Flux<Account> createAccounts(Account newAccount);

Request Processor

You can configure request processors on every Client. These processors will be called on every request created by the client.

public class ContentTypeRequestProcessor implements RequestProcessor {
    @Override public ClientRequest accept(ClientRequest clientRequest) {
        return ClientRequest.from(clientRequest)
                            .headers(headers -> headers
                                .set(HttpHeaders.CONTENT_TYPE, "application/json"))
                            .build();
    }
}
...
AccountClient accountClient = return ClientBuilder
    .builder()
    .requestProcessor(new ContentTypeRequestProcessor())
    .build(HelloClient.class, "http://example.com");

Response Processor

You can configure response interceptors on every Client. These interceptors will be called on every response the client receive.

public class ConsoleLogResponseProcessor implements ResponseProcessor {
    @Override public ClientResponse accept(ClientResponse clientResponse) {
        System.out.println(clientResponse);
        return clientResponse;
    }
}
...
AccountClient accountClient = return ClientBuilder
    .builder()
    .responseProcessor(new ConsoleLogResponseProcessor())
    .build(HelloClient.class, "http://example.com");

Codecs

There is 3 kinds of codecs you can configure within the ClientBuilder:

  • HttpMessageWriter
  • HttpMessageReader
  • HttpErrorReader

HttpMessageWriter and HttpMessageReader standard codecs used within the Spring-webflux.

  1. HttpErrorReader

These readers are used when the response status code is 4xx or 5xx so you can deserialize custom error payload.

By default there is 2 HttpErrorReader that will process the response and wrap the body as String.

  • Statuses 5xx response will be wrap in a HttpServerException
  • Statuses 4xx response will be wrap in a HttpServerException
  1. Override default codecs.

During the building phase of your client you can configure some of the default codec decoders used by the client.

ClientBuilder.builder()
    .defaultCodecs(defaultCodecsConfigurerConsumer -> {
        defaultCodecsConfigurerConsumer.jackson2Encoder(new CustomJackson2JsonEncoder());
        defaultCodecsConfigurerConsumer.jackson2Decoder(new CustomJackson2JsonDecoder());
        defaultCodecsConfigurerConsumer.serverSentEventDecoder(new CustomDecoder());
        defaultCodecsConfigurerConsumer.clientErrorDecoder(new CustomClientErrorDecoder());
        defaultCodecsConfigurerConsumer.serverErrorDecoder(new CustomServerErrorDecoder);
    })
    .build(HelloClient.class, "http://example.com");
  1. Configure custom codecs.

During the building phase of your client you can configure add custom codecs to fit your specifics needs.

3.1 HttpMessageWriter, HttpMessageReader and HttpErrorReader

ClientBuilder.builder()
    .customCodecs(customCodecs -> {
        customCodecs.reader(new CustomHttpMessageReader());
        customCodecs.writer(new CustomHttpMessageWriter());
        customCodecs.errorReader(new CustomHttpErrorReader());
    })
    .build(HelloClient.class, "http://example.com");

3.2 Encoder, Decoder and ErrorDecoder

ClientBuilder.builder()
    .customCodecs(customCodecs -> {
        customCodecs.decoder(new CustomDecoder());
        customCodecs.encoder(new CustomEncoder());
        customCodecs.errorDecoder(new CustomErrorDecoder());
    })
    .build(HelloClient.class, "http://example.com");

spring-webflux-client's People

Watchers

Guru avatar

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.