From cabdb8f2d1fc06e7ba801a98f0924eec922f2a77 Mon Sep 17 00:00:00 2001 From: neha298 <32795246+neha298@users.noreply.github.com> Date: Sun, 12 Mar 2023 20:57:43 +0530 Subject: [PATCH 1/6] Configure feign client URL --- .../cloud/openfeign/client/AlbumClient.java | 15 ++++ .../cloud/openfeign/client/PostClient.java | 12 +++ .../cloud/openfeign/client/TodoClient.java | 13 +++ .../config/DynamicUrlInterceptor.java | 23 ++++++ .../ConfigureFeignUrlController.java | 80 +++++++++++++++++++ .../baeldung/cloud/openfeign/model/Album.java | 32 ++++++++ .../baeldung/cloud/openfeign/model/Todo.java | 40 ++++++++++ .../src/main/resources/application.properties | 2 + 8 files changed, 217 insertions(+) create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/AlbumClient.java create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/PostClient.java create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/TodoClient.java create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/config/DynamicUrlInterceptor.java create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Album.java create mode 100644 spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Todo.java diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/AlbumClient.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/AlbumClient.java new file mode 100644 index 0000000000..b1db2dbcab --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/AlbumClient.java @@ -0,0 +1,15 @@ +package com.baeldung.cloud.openfeign.client; + +import com.baeldung.cloud.openfeign.model.Album; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +@FeignClient(name = "albumClient", url = "https://jsonplaceholder.typicode.com/albums/") +public interface AlbumClient { + @GetMapping(value = "/{id}") + Album getAlbumById(@PathVariable(value = "id") Integer id); + + @GetMapping(value = "/{id}") + Album getAlbumByIdAndDynamicUrl(@PathVariable(name = "id") Integer id); +} diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/PostClient.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/PostClient.java new file mode 100644 index 0000000000..e8e773b6a1 --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/PostClient.java @@ -0,0 +1,12 @@ +package com.baeldung.cloud.openfeign.client; + +import com.baeldung.cloud.openfeign.model.Post; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +@FeignClient(name = "postClient", url = "${spring.cloud.openfeign.client.config.postClient.url}") +public interface PostClient { + @GetMapping(value = "/{id}") + Post getPostById(@PathVariable(value = "id") Integer id); +} diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/TodoClient.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/TodoClient.java new file mode 100644 index 0000000000..c768ef6b5f --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/client/TodoClient.java @@ -0,0 +1,13 @@ +package com.baeldung.cloud.openfeign.client; + +import com.baeldung.cloud.openfeign.model.Todo; +import feign.RequestLine; +import org.springframework.cloud.openfeign.FeignClient; + +import java.net.URI; + +@FeignClient(name = "todoClient") +public interface TodoClient { + @RequestLine(value = "GET") + Todo getTodoById(URI uri); +} diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/config/DynamicUrlInterceptor.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/config/DynamicUrlInterceptor.java new file mode 100644 index 0000000000..286426edb3 --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/config/DynamicUrlInterceptor.java @@ -0,0 +1,23 @@ +package com.baeldung.cloud.openfeign.config; + +import feign.RequestInterceptor; +import feign.RequestTemplate; + +import java.util.function.Supplier; + +public class DynamicUrlInterceptor implements RequestInterceptor { + + private final Supplier urlSupplier; + + public DynamicUrlInterceptor(Supplier urlSupplier) { + this.urlSupplier = urlSupplier; + } + + @Override + public void apply(RequestTemplate template) { + String url = urlSupplier.get(); + if (url != null) { + template.target(url); + } + } +} diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java new file mode 100644 index 0000000000..c048e7e1f7 --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java @@ -0,0 +1,80 @@ +package com.baeldung.cloud.openfeign.controller; + +import com.baeldung.cloud.openfeign.config.DynamicUrlInterceptor; +import com.baeldung.cloud.openfeign.client.AlbumClient; +import com.baeldung.cloud.openfeign.client.PostClient; +import com.baeldung.cloud.openfeign.client.TodoClient; +import com.baeldung.cloud.openfeign.model.Album; +import com.baeldung.cloud.openfeign.model.Post; +import com.baeldung.cloud.openfeign.model.Todo; +import feign.Feign; +import feign.Target; +import feign.codec.Decoder; +import feign.codec.Encoder; +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.http.HttpMessageConverters; +import org.springframework.cloud.openfeign.FeignClientsConfiguration; +import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer; +import org.springframework.cloud.openfeign.support.SpringDecoder; +import org.springframework.cloud.openfeign.support.SpringEncoder; +import org.springframework.cloud.openfeign.support.SpringMvcContract; +import org.springframework.context.annotation.Import; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URI; + +@RestController +@Import(FeignClientsConfiguration.class) +public class ConfigureFeignUrlController { + private final AlbumClient albumClient; + private final PostClient postClient; + + private final TodoClient todoClient; + private final ObjectFactory messageConverters; + + private final ObjectProvider customizers; + + public ConfigureFeignUrlController(AlbumClient albumClient, + PostClient postClient, + Decoder decoder, + Encoder encoder, + ObjectFactory messageConverters, ObjectProvider customizers) { + this.albumClient = albumClient; + this.postClient = postClient; + this.messageConverters = messageConverters; + this.customizers = customizers; + this.todoClient = Feign.builder().encoder(encoder).decoder(decoder).target(Target.EmptyTarget.create(TodoClient.class)); + } + + @GetMapping(value = "albums/{id}") + public Album getAlbumById(@PathVariable(value = "id") Integer id) { + return albumClient.getAlbumById(id); + } + + @GetMapping(value = "posts/{id}") + public Post getPostById(@PathVariable(value = "id") Integer id) { + return postClient.getPostById(id); + } + + @GetMapping(value = "todos/{id}") + public Todo getTodoById(@PathVariable(value = "id") Integer id) { + return todoClient.getTodoById(URI.create("https://jsonplaceholder.typicode.com/todos/" + id)); + } + +@GetMapping(value = "/dynamicAlbums/{id}") +public Album getAlbumByIdAndDynamicUrl(@PathVariable(value = "id") Integer id) { + AlbumClient client = Feign.builder() + .requestInterceptor(new DynamicUrlInterceptor(() -> "https://jsonplaceholder.typicode.com/albums/")) + .contract(new SpringMvcContract()) + .encoder(new SpringEncoder(messageConverters)) + .decoder(new SpringDecoder(messageConverters, customizers)) + .target(Target.EmptyTarget.create(AlbumClient.class)); + + return client.getAlbumByIdAndDynamicUrl(id); +} +} + + diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Album.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Album.java new file mode 100644 index 0000000000..ae9c01f3b1 --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Album.java @@ -0,0 +1,32 @@ +package com.baeldung.cloud.openfeign.model; + +public class Album { + + private Integer id; + private Integer userId; + private String title; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Todo.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Todo.java new file mode 100644 index 0000000000..887a3496c5 --- /dev/null +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/model/Todo.java @@ -0,0 +1,40 @@ +package com.baeldung.cloud.openfeign.model; + +public class Todo { + private Integer id; + private Integer userId; + private String title; + private Boolean completed; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Boolean getCompleted() { + return completed; + } + + public void setCompleted(Boolean completed) { + this.completed = completed; + } +} diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/resources/application.properties b/spring-cloud-modules/spring-cloud-openfeign/src/main/resources/application.properties index a0d9d89934..f4ea32483d 100644 --- a/spring-cloud-modules/spring-cloud-openfeign/src/main/resources/application.properties +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/resources/application.properties @@ -8,3 +8,5 @@ spring.security.oauth2.client.registration.keycloak.authorization-grant-type=cli spring.security.oauth2.client.registration.keycloak.client-id=payment-app spring.security.oauth2.client.registration.keycloak.client-secret=863e9de4-33d4-4471-b35e-f8d2434385bb spring.security.oauth2.client.provider.keycloak.token-uri=http://localhost:8083/auth/realms/master/protocol/openid-connect/token + +spring.cloud.openfeign.client.config.postClient.url=https://jsonplaceholder.typicode.com/posts/ \ No newline at end of file From 53cb3564c9eeb6d5a90636c7d47a556418e6689c Mon Sep 17 00:00:00 2001 From: neha298 <32795246+neha298@users.noreply.github.com> Date: Sun, 12 Mar 2023 21:54:30 +0530 Subject: [PATCH 2/6] Spacing modifications --- .../ConfigureFeignUrlController.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java index c048e7e1f7..ca49bca605 100644 --- a/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java +++ b/spring-cloud-modules/spring-cloud-openfeign/src/main/java/com/baeldung/cloud/openfeign/controller/ConfigureFeignUrlController.java @@ -30,9 +30,11 @@ import java.net.URI; @Import(FeignClientsConfiguration.class) public class ConfigureFeignUrlController { private final AlbumClient albumClient; + private final PostClient postClient; private final TodoClient todoClient; + private final ObjectFactory messageConverters; private final ObjectProvider customizers; @@ -64,17 +66,17 @@ public class ConfigureFeignUrlController { return todoClient.getTodoById(URI.create("https://jsonplaceholder.typicode.com/todos/" + id)); } -@GetMapping(value = "/dynamicAlbums/{id}") -public Album getAlbumByIdAndDynamicUrl(@PathVariable(value = "id") Integer id) { - AlbumClient client = Feign.builder() - .requestInterceptor(new DynamicUrlInterceptor(() -> "https://jsonplaceholder.typicode.com/albums/")) - .contract(new SpringMvcContract()) - .encoder(new SpringEncoder(messageConverters)) - .decoder(new SpringDecoder(messageConverters, customizers)) - .target(Target.EmptyTarget.create(AlbumClient.class)); + @GetMapping(value = "/dynamicAlbums/{id}") + public Album getAlbumByIdAndDynamicUrl(@PathVariable(value = "id") Integer id) { + AlbumClient client = Feign.builder() + .requestInterceptor(new DynamicUrlInterceptor(() -> "https://jsonplaceholder.typicode.com/albums/")) + .contract(new SpringMvcContract()) + .encoder(new SpringEncoder(messageConverters)) + .decoder(new SpringDecoder(messageConverters, customizers)) + .target(Target.EmptyTarget.create(AlbumClient.class)); - return client.getAlbumByIdAndDynamicUrl(id); -} + return client.getAlbumByIdAndDynamicUrl(id); + } } From d1da4b149413d8bff2918e201d44d4a32f5c7180 Mon Sep 17 00:00:00 2001 From: neha298 <32795246+neha298@users.noreply.github.com> Date: Sun, 16 Apr 2023 23:42:27 +0530 Subject: [PATCH 3/6] Fastexcel --- apache-poi/pom.xml | 11 ++++ .../baeldung/fastexcel/FastexcelHelper.java | 64 +++++++++++++++++++ .../fastexcel/FastexcelIntegrationTest.java | 50 +++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java create mode 100644 apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml index 546eedec5b..78781cf215 100644 --- a/apache-poi/pom.xml +++ b/apache-poi/pom.xml @@ -30,6 +30,16 @@ + + org.dhatim + fastexcel + ${fastexcel.version} + + + org.dhatim + fastexcel-reader + ${fastexcel.version} + @@ -52,6 +62,7 @@ 5.2.0 1.0.6 + 0.15.3 3.2.0 diff --git a/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java new file mode 100644 index 0000000000..524f345184 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java @@ -0,0 +1,64 @@ +package com.baeldung.fastexcel; + +import org.dhatim.fastexcel.Workbook; +import org.dhatim.fastexcel.Worksheet; +import org.dhatim.fastexcel.reader.Cell; +import org.dhatim.fastexcel.reader.ReadableWorkbook; +import org.dhatim.fastexcel.reader.Row; +import org.dhatim.fastexcel.reader.Sheet; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +public class FastexcelHelper { + + public Map> readExcel(String fileLocation) throws IOException { + Map> data = new HashMap<>(); + + try (FileInputStream file = new FileInputStream(fileLocation); ReadableWorkbook wb = new ReadableWorkbook(file)) { + Sheet sheet = wb.getFirstSheet(); + try (Stream rows = sheet.openStream()) { + rows.forEach(r -> { + data.put(r.getRowNum(), new ArrayList<>()); + + for (Cell cell : r) { + data.get(r.getRowNum()).add(cell.getRawValue()); + } + }); + } + } + + return data; + } + + public void writeExcel() throws IOException { + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + String fileLocation = path.substring(0, path.length() - 1) + "fastexcel.xlsx"; + + try (OutputStream os = Files.newOutputStream(Paths.get(fileLocation)); Workbook wb = new Workbook(os, "MyApplication", "1.0")) { + Worksheet ws = wb.newWorksheet("Sheet 1"); + + ws.width(0, 25); + ws.width(1, 15); + + + ws.range(0, 0, 0, 1).style().fontName("Arial").fontSize(16).bold().fillColor("3366FF").set(); + ws.value(0, 0, "Name"); + ws.value(0, 1, "Age"); + + ws.range(1, 0, 1, 1).style().wrapText(true).set(); + ws.value(2, 0, "John Smith"); + ws.value(2, 1, 20L); + } + } +} diff --git a/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java new file mode 100644 index 0000000000..8c1d568b25 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java @@ -0,0 +1,50 @@ +package com.baeldung.fastexcel; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class FastexcelIntegrationTest { + + private FastexcelHelper fastexcelHelper; + private static String FILE_NAME = "fastexcel.xlsx"; + private String fileLocation; + + @Before + public void generateExcelFile() throws IOException { + + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + fileLocation = path.substring(0, path.length() - 1) + FILE_NAME; + System.out.println(fileLocation); + + fastexcelHelper = new FastexcelHelper(); + fastexcelHelper.writeExcel(); + } + + @Test + public void whenParsingExcelFile_thenCorrect() throws IOException { + Map> data = fastexcelHelper.readExcel(fileLocation); + + assertEquals("Name", data.get(1).get(0)); + assertEquals("Age", data.get(1).get(1)); + + assertEquals("John Smith", data.get(3).get(0)); + assertEquals("20", data.get(3).get(1)); + } + + @After + public void cleanup() { + File testFile = new File(fileLocation); + if (testFile.exists()) { + testFile.delete(); + } + } +} \ No newline at end of file From c9770d8dd545a30f0804b8c507a9edd48ed953d3 Mon Sep 17 00:00:00 2001 From: neha298 <32795246+neha298@users.noreply.github.com> Date: Mon, 17 Apr 2023 01:01:42 +0530 Subject: [PATCH 4/6] Removed extra whitespace --- .../src/main/java/com/baeldung/fastexcel/FastexcelHelper.java | 1 - 1 file changed, 1 deletion(-) diff --git a/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java index 524f345184..fd5492359a 100644 --- a/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java +++ b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java @@ -51,7 +51,6 @@ public class FastexcelHelper { ws.width(0, 25); ws.width(1, 15); - ws.range(0, 0, 0, 1).style().fontName("Arial").fontSize(16).bold().fillColor("3366FF").set(); ws.value(0, 0, "Name"); ws.value(0, 1, "Age"); From f2c036a59d3ebfe1bf46a7fad0570b23dd33a90a Mon Sep 17 00:00:00 2001 From: neha298 <32795246+neha298@users.noreply.github.com> Date: Mon, 17 Apr 2023 01:11:46 +0530 Subject: [PATCH 5/6] Removed print statement --- .../java/com/baeldung/fastexcel/FastexcelIntegrationTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java index 8c1d568b25..a36c06e41f 100644 --- a/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java +++ b/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java @@ -23,7 +23,6 @@ public class FastexcelIntegrationTest { File currDir = new File("."); String path = currDir.getAbsolutePath(); fileLocation = path.substring(0, path.length() - 1) + FILE_NAME; - System.out.println(fileLocation); fastexcelHelper = new FastexcelHelper(); fastexcelHelper.writeExcel(); @@ -47,4 +46,4 @@ public class FastexcelIntegrationTest { testFile.delete(); } } -} \ No newline at end of file +} From 83563728377de9c4448b463ad9a2e99640ae1501 Mon Sep 17 00:00:00 2001 From: neha298 <32795246+neha298@users.noreply.github.com> Date: Mon, 17 Apr 2023 01:26:06 +0530 Subject: [PATCH 6/6] Row index modification --- .../src/main/java/com/baeldung/fastexcel/FastexcelHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java index fd5492359a..9d6041b66e 100644 --- a/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java +++ b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java @@ -55,7 +55,7 @@ public class FastexcelHelper { ws.value(0, 0, "Name"); ws.value(0, 1, "Age"); - ws.range(1, 0, 1, 1).style().wrapText(true).set(); + ws.range(2, 0, 2, 1).style().wrapText(true).set(); ws.value(2, 0, "John Smith"); ws.value(2, 1, 20L); }