From 107866becb7f2bcc8425eeaaecf11e945af2ac47 Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr Date: Thu, 20 Jun 2024 20:57:39 +0200 Subject: [PATCH] Create Customer Api Consumer --- .github/workflows/maven.yml | 7 +- customer-api-consumer/pom.xml | 153 ++++++++++++++++++ .../consumer/CustomerConsumerApplication.java | 13 ++ .../sample/consumer/CustomerService.java | 20 +++ .../sample/consumer/CustomersResource.java | 31 ++++ .../sample/consumer/client/CustomerApi.java | 22 +++ .../consumer/client/CustomerApiProvider.java | 19 +++ .../sample/consumer/client/CustomerDto.java | 26 +++ .../src/main/resources/application.properties | 4 + 9 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 customer-api-consumer/pom.xml create mode 100644 customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerConsumerApplication.java create mode 100644 customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerService.java create mode 100644 customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomersResource.java create mode 100644 customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApi.java create mode 100644 customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApiProvider.java create mode 100644 customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerDto.java create mode 100644 customer-api-consumer/src/main/resources/application.properties diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 6cf8675..b2d9fcb 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -6,12 +6,14 @@ on: - "main" paths: - "customer-api-provider/**" + - "customer-api-consumer/**" - ".github/**" pull_request: branches: - "main" paths: - "customer-api-provider/**" + - "customer-api-consumer/**" - ".github/**" jobs: @@ -27,6 +29,9 @@ jobs: java-version: '21' distribution: 'temurin' cache: maven - - name: Build with Maven + - name: Build API Provider with Maven working-directory: customer-api-provider run: mvn -B package --file pom.xml + - name: Build API Consumer with Maven + working-directory: customer-api-consumer + run: mvn -B package --file pom.xml diff --git a/customer-api-consumer/pom.xml b/customer-api-consumer/pom.xml new file mode 100644 index 0000000..cb6b680 --- /dev/null +++ b/customer-api-consumer/pom.xml @@ -0,0 +1,153 @@ + + + 4.0.0 + de.schulung.quarkus + customer-api-consumer + 1.0.0-SNAPSHOT + + + 3.12.1 + 21 + UTF-8 + UTF-8 + quarkus-bom + io.quarkus.platform + 3.11.2 + true + 3.2.5 + 1.18.32 + + + + + + io.quarkus.platform + quarkus-bom + ${quarkus.platform.version} + pom + import + + + + + + + org.projectlombok + lombok + ${lombok.version} + provided + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-resteasy-reactive + + + io.quarkus + quarkus-rest-client-reactive-jsonb + + + io.quarkus + quarkus-junit5 + test + + + io.quarkus + quarkus-junit5-mockito + test + + + + + + + + maven-compiler-plugin + ${compiler-plugin.version} + + + + org.projectlombok + lombok + ${lombok.version} + + + + + + + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus.platform.version} + true + + + + build + generate-code + generate-code-tests + + + + + + maven-compiler-plugin + + + -parameters + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + + native + + + native + + + + false + true + + + + diff --git a/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerConsumerApplication.java b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerConsumerApplication.java new file mode 100644 index 0000000..edd116b --- /dev/null +++ b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerConsumerApplication.java @@ -0,0 +1,13 @@ +package de.schulung.sample.consumer; + +import io.quarkus.runtime.Quarkus; +import io.quarkus.runtime.annotations.QuarkusMain; + +@QuarkusMain +public class CustomerConsumerApplication { + + public static void main(String[] args) { + Quarkus.run(args); + } + +} diff --git a/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerService.java b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerService.java new file mode 100644 index 0000000..f523b73 --- /dev/null +++ b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomerService.java @@ -0,0 +1,20 @@ +package de.schulung.sample.consumer; + +import de.schulung.sample.consumer.client.CustomerApi; +import de.schulung.sample.consumer.client.CustomerDto; +import io.smallrye.mutiny.Uni; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.RequiredArgsConstructor; + +import java.util.Collection; + +@ApplicationScoped +@RequiredArgsConstructor +public class CustomerService { + + private final CustomerApi customerApi; + + public Uni> getAllCustomers() { // quick'n'dirty without mapping! + return customerApi.getAllCustomers(); + } +} diff --git a/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomersResource.java b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomersResource.java new file mode 100644 index 0000000..ca9224c --- /dev/null +++ b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/CustomersResource.java @@ -0,0 +1,31 @@ +package de.schulung.sample.consumer; + +import de.schulung.sample.consumer.client.CustomerDto; +import io.smallrye.mutiny.Uni; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import lombok.RequiredArgsConstructor; + +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@Path("/customers") +public class CustomersResource { + + private final CustomerService service; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public Uni getCustomerNames() { + return this.service + .getAllCustomers() + .map(list -> list + .stream() + .map(CustomerDto::getName) + .collect(Collectors.joining("\n")) + ); + } + +} diff --git a/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApi.java b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApi.java new file mode 100644 index 0000000..a4bfda0 --- /dev/null +++ b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApi.java @@ -0,0 +1,22 @@ +package de.schulung.sample.consumer.client; + +import io.smallrye.mutiny.Uni; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; + +import java.util.Collection; + +@RegisterRestClient(configKey = "customer-api") +@Path("/customers") +public interface CustomerApi { + + // https://quarkus.io/guides/rest-client + + @GET + @Produces(MediaType.APPLICATION_JSON) + Uni> getAllCustomers(); + +} diff --git a/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApiProvider.java b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApiProvider.java new file mode 100644 index 0000000..61b1efe --- /dev/null +++ b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerApiProvider.java @@ -0,0 +1,19 @@ +package de.schulung.sample.consumer.client; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Default; +import jakarta.enterprise.inject.Produces; +import lombok.Getter; +import org.eclipse.microprofile.rest.client.inject.RestClient; + +@ApplicationScoped +public class CustomerApiProvider { + + @RestClient + @Getter(onMethod_ = { + @Produces, + @Default + }) + CustomerApi customerApi; + +} diff --git a/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerDto.java b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerDto.java new file mode 100644 index 0000000..99d738b --- /dev/null +++ b/customer-api-consumer/src/main/java/de/schulung/sample/consumer/client/CustomerDto.java @@ -0,0 +1,26 @@ +package de.schulung.sample.consumer.client; + +import jakarta.json.bind.annotation.JsonbTransient; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalDate; +import java.util.UUID; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class CustomerDto { + + // readonly property + @Setter(onMethod_ = @JsonbTransient) + private UUID uuid; + private String name; + //@JsonbProperty("birth_date") + private LocalDate birthDate; + private String state; + +} diff --git a/customer-api-consumer/src/main/resources/application.properties b/customer-api-consumer/src/main/resources/application.properties new file mode 100644 index 0000000..39776b5 --- /dev/null +++ b/customer-api-consumer/src/main/resources/application.properties @@ -0,0 +1,4 @@ +%dev.quarkus.http.port=8081 +%dev.quarkus.rest-client."customer-api".url=http://localhost:8080 +quarkus.rest-client."customer-api".connect-timeout=2000 +%dev.quarkus.rest-client."customer-api".read-timeout=2000 \ No newline at end of file