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

driven-adapter.consumer-rest.reactive-rest-consumer.rest-consumer.mustache Maven / Gradle / Ivy

Go to download

Gradle plugin to create a clean application in Java that already works, It follows our best practices!

There is a newer version: 3.20.10
Show newest version
package {{package}}.consumer;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
{{#lombok}}
import lombok.RequiredArgsConstructor;
{{/lombok}}
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Service
{{#lombok}}
@RequiredArgsConstructor
{{/lombok}}
public class RestConsumer /* implements Gateway from domain */{
    private final WebClient client;

{{^lombok}}
    public RestConsumer(WebClient client) {
        this.client = client;
    }
{{/lombok}}

    // these methods are an example that illustrates the implementation of WebClient.
    // You should use the methods that you implement from the Gateway from the domain.
    @CircuitBreaker(name = "testGet" /*, fallbackMethod = "testGetOk"*/)
    public Mono testGet() {
        return client
                .get()
                .retrieve()
                .bodyToMono(ObjectResponse.class);
    }

// Possible fallback method
//    public Mono testGetOk(Exception ignored) {
//        return client
//                .get() // TODO: change for another endpoint or destination
//                .retrieve()
//                .bodyToMono(String.class);
//    }

    @CircuitBreaker(name = "testPost")
    public Mono testPost() {
    {{#lombok}}
        ObjectRequest request = ObjectRequest.builder()
            .val1("exampleval1")
            .val2("exampleval2")
            .build();
    {{/lombok}}
    {{^lombok}}
        ObjectRequest request = new ObjectRequest();
        request.setVal1("exampleval1");
        request.setVal2("exampleval2");
    {{/lombok}}
        return client
                .post()
                .body(Mono.just(request), ObjectRequest.class)
                .retrieve()
                .bodyToMono(ObjectResponse.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy