-
Notifications
You must be signed in to change notification settings - Fork 54k
BAEL-7272: Added code for custom deserialization using Spring WebClient #15702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
286cfd1
BAEL-7272: Added code for custom deserialization using Spring WebClient
balasr3 e5e668e
BAEL-7272: Added newline
balasr3 d7d7c1e
BAEL-7272: Fixed based on review comments
balasr3 869239f
BAEL-7272: modified test name
balasr3 cf94bbc
BAEL-7272: modified test name
balasr3 983fc30
Merge branch 'master' into master
davidmartinezbarua 7727131
Merge branch 'eugenp:master' into master
balasr21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ules/spring-reactive-3/src/main/java/com/baeldung/custom/deserialization/Application.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.baeldung.custom.deserialization; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(Application.class, args); | ||
| } | ||
|
|
||
| } |
27 changes: 27 additions & 0 deletions
27
...ive-3/src/main/java/com/baeldung/custom/deserialization/config/CodecCustomizerConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.baeldung.custom.deserialization.config; | ||
|
|
||
| import org.springframework.boot.web.codec.CodecCustomizer; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.codec.CodecConfigurer; | ||
| import org.springframework.http.codec.json.Jackson2JsonDecoder; | ||
| import org.springframework.http.codec.json.Jackson2JsonEncoder; | ||
| import org.springframework.util.MimeType; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| @Configuration | ||
| public class CodecCustomizerConfig { | ||
|
|
||
| @Bean | ||
| public CodecCustomizer codecCustomizer(ObjectMapper customObjectMapper) { | ||
| return configurer -> { | ||
| MimeType mimeType = MimeType.valueOf(MediaType.APPLICATION_JSON_VALUE); | ||
| CodecConfigurer.CustomCodecs customCodecs = configurer.customCodecs(); | ||
| customCodecs.register(new Jackson2JsonDecoder(customObjectMapper, mimeType)); | ||
| customCodecs.register(new Jackson2JsonEncoder(customObjectMapper, mimeType)); | ||
| }; | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
...active-3/src/main/java/com/baeldung/custom/deserialization/config/CustomDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.baeldung.custom.deserialization.config; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.LocalDateTime; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.ZoneOffset; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | ||
|
|
||
| public class CustomDeserializer extends LocalDateTimeDeserializer { | ||
| @Override | ||
| public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException { | ||
| try { | ||
| return OffsetDateTime.parse(jsonParser.getText()).atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime(); | ||
| } catch (Exception e) { | ||
| return super.deserialize(jsonParser, ctxt); | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...active-3/src/main/java/com/baeldung/custom/deserialization/config/CustomObjectMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.baeldung.custom.deserialization.config; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import com.fasterxml.jackson.databind.DeserializationFeature; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
|
||
| @Configuration | ||
| public class CustomObjectMapper { | ||
| @Bean | ||
| public ObjectMapper objectMapper() { | ||
| return new ObjectMapper() | ||
| .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) | ||
| .registerModule(new JavaTimeModule()); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...ctive-3/src/main/java/com/baeldung/custom/deserialization/controller/OrderController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.baeldung.custom.deserialization.controller; | ||
|
|
||
| import org.springframework.http.MediaType; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.baeldung.custom.deserialization.model.OrderResponse; | ||
| import com.baeldung.custom.deserialization.service.ExternalServiceV1; | ||
| import com.baeldung.custom.deserialization.service.ExternalServiceV2; | ||
|
|
||
| import reactor.core.publisher.Mono; | ||
|
|
||
| @RestController | ||
| public class OrderController { | ||
|
|
||
| private final ExternalServiceV1 externalServiceV1; | ||
| private final ExternalServiceV2 externalServiceV2; | ||
|
|
||
| public OrderController(ExternalServiceV1 externalServiceV1, ExternalServiceV2 externalServiceV2) { | ||
| this.externalServiceV1 = externalServiceV1; | ||
| this.externalServiceV2 = externalServiceV2; | ||
| } | ||
|
|
||
| @GetMapping(value = "v1/order/{id}", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public final Mono<OrderResponse> searchOrderV1(@PathVariable(value = "id") int id) { | ||
| return externalServiceV1.findById(id) | ||
| .bodyToMono(OrderResponse.class); | ||
| } | ||
|
|
||
| @GetMapping(value = "v2/order/{id}", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public final Mono<OrderResponse> searchOrderV2(@PathVariable(value = "id") int id) { | ||
| return externalServiceV2.findById(id) | ||
| .bodyToMono(OrderResponse.class); | ||
| } | ||
|
|
||
| } |
19 changes: 19 additions & 0 deletions
19
...ing-reactive-3/src/main/java/com/baeldung/custom/deserialization/model/OrderResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.baeldung.custom.deserialization.model; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class OrderResponse { | ||
|
|
||
| private UUID orderId; | ||
|
|
||
| private LocalDateTime orderDateTime; | ||
|
|
||
| private List<String> address; | ||
|
|
||
| private List<String> orderNotes; | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
...active-3/src/main/java/com/baeldung/custom/deserialization/service/ExternalServiceV1.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.baeldung.custom.deserialization.service; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| @Component | ||
| public class ExternalServiceV1 { | ||
|
|
||
| private final WebClient.Builder webclientBuilder; | ||
|
|
||
| public ExternalServiceV1(WebClient.Builder webclientBuilder) { | ||
| this.webclientBuilder = webclientBuilder; | ||
| } | ||
|
|
||
| public WebClient.ResponseSpec findById(int id) { | ||
| return webclientBuilder.baseUrl("http://localhost:8090/") | ||
| .build() | ||
| .get() | ||
| .uri("external/order/" + id) | ||
| .retrieve(); | ||
| } | ||
|
|
||
| } |
40 changes: 40 additions & 0 deletions
40
...active-3/src/main/java/com/baeldung/custom/deserialization/service/ExternalServiceV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.baeldung.custom.deserialization.service; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.codec.json.Jackson2JsonDecoder; | ||
| import org.springframework.http.codec.json.Jackson2JsonEncoder; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.reactive.function.client.ExchangeStrategies; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| import com.baeldung.custom.deserialization.config.CustomDeserializer; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
|
||
| @Component | ||
| public class ExternalServiceV2 { | ||
|
|
||
| public WebClient.ResponseSpec findById(int id) { | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalDateTime.class, new CustomDeserializer())); | ||
|
|
||
| WebClient webClient = WebClient.builder() | ||
| .baseUrl("http://localhost:8090/") | ||
| .exchangeStrategies(ExchangeStrategies.builder() | ||
| .codecs(clientDefaultCodecsConfigurer -> { | ||
| clientDefaultCodecsConfigurer.defaultCodecs() | ||
| .jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON)); | ||
| clientDefaultCodecsConfigurer.defaultCodecs() | ||
| .jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON)); | ||
| }) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| return webClient.get() | ||
| .uri("external/order/" + id) | ||
| .retrieve(); | ||
| } | ||
|
|
||
| } |
135 changes: 135 additions & 0 deletions
135
...e-3/src/test/java/com/baeldung/custom/deserialization/OrderControllerIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package com.baeldung.custom.deserialization; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
|
|
||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.test.context.TestPropertySource; | ||
| import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
|
||
| import com.baeldung.custom.deserialization.model.OrderResponse; | ||
|
|
||
| import okhttp3.mockwebserver.MockResponse; | ||
| import okhttp3.mockwebserver.MockWebServer; | ||
|
|
||
| @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) | ||
| @TestPropertySource(properties = "server.port=8091") | ||
| @AutoConfigureWebTestClient(timeout = "100000") | ||
| class OrderControllerIntegrationTest { | ||
|
|
||
| @Autowired | ||
| private WebTestClient webTestClient; | ||
|
|
||
| private static MockWebServer mockExternalService; | ||
|
|
||
| @BeforeAll | ||
| static void setup() throws IOException { | ||
| mockExternalService = new MockWebServer(); | ||
| mockExternalService.start(8090); | ||
| } | ||
|
|
||
| @Test | ||
| void givenMockedExternalResponse_whenSearchByIdV1_thenOrderResponseShouldFailBecauseOfUnknownProperty() { | ||
|
|
||
| mockExternalService.enqueue(new MockResponse().addHeader("Content-Type", "application/json; charset=utf-8") | ||
| .setBody("{\n" + " \"orderId\": \"a1b2c3d4-e5f6-4a5b-8c9d-0123456789ab\",\n" | ||
| + " \"orderDateTime\": \"2024-01-20T12:34:56\",\n" | ||
| + " \"address\": [\"123 Main St\", \"Apt 456\", \"Cityville\"],\n" | ||
| + " \"orderNotes\": [\"Special request: Handle with care\", \"Gift wrapping required\"],\n" | ||
| + " \"customerName\": \"John Doe\",\n" + " \"totalAmount\": 99.99,\n" | ||
| + " \"paymentMethod\": \"Credit Card\"\n" + " }") | ||
| .setResponseCode(HttpStatus.OK.value())); | ||
|
|
||
| webTestClient.get() | ||
| .uri("v1/order/1") | ||
| .exchange() | ||
| .expectStatus() | ||
| .is5xxServerError(); | ||
| } | ||
|
|
||
| @Test | ||
| void givenMockedExternalResponse_whenSearchByIdV1_thenOrderResponseShouldBeReceivedSuccessfully() { | ||
|
|
||
| mockExternalService.enqueue(new MockResponse().addHeader("Content-Type", "application/json; charset=utf-8") | ||
| .setBody("{\n" + " \"orderId\": \"a1b2c3d4-e5f6-4a5b-8c9d-0123456789ab\",\n" | ||
| + " \"orderDateTime\": \"2024-01-20T12:34:56\",\n" | ||
| + " \"address\": [\"123 Main St\", \"Apt 456\", \"Cityville\"],\n" | ||
| + " \"orderNotes\": [\"Special request: Handle with care\", \"Gift wrapping required\"]\n" | ||
| + " }") | ||
| .setResponseCode(HttpStatus.OK.value())); | ||
|
|
||
| OrderResponse orderResponse = webTestClient.get() | ||
| .uri("v1/order/1") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectBody(OrderResponse.class) | ||
| .returnResult() | ||
| .getResponseBody(); | ||
| assertEquals(UUID.fromString("a1b2c3d4-e5f6-4a5b-8c9d-0123456789ab"), orderResponse.getOrderId()); | ||
| assertEquals(LocalDateTime.of(2024, 1, 20, 12, 34, 56), orderResponse.getOrderDateTime()); | ||
| assertThat(orderResponse.getAddress()).hasSize(3); | ||
| assertThat(orderResponse.getOrderNotes()).hasSize(2); | ||
| } | ||
|
|
||
| @Test | ||
| void givenMockedExternalResponse_whenSearchByIdV2_thenOrderResponseShouldFailBecauseOfUnknownProperty() { | ||
|
|
||
| mockExternalService.enqueue(new MockResponse().addHeader("Content-Type", "application/json; charset=utf-8") | ||
| .setBody("{\n" + " \"orderId\": \"a1b2c3d4-e5f6-4a5b-8c9d-0123456789ab\",\n" | ||
| + " \"orderDateTime\": \"2024-01-20T12:34:56\",\n" | ||
| + " \"address\": [\"123 Main St\", \"Apt 456\", \"Cityville\"],\n" | ||
| + " \"orderNotes\": [\"Special request: Handle with care\", \"Gift wrapping required\"],\n" | ||
| + " \"customerName\": \"John Doe\",\n" | ||
| + " \"totalAmount\": 99.99,\n" | ||
| + " \"paymentMethod\": \"Credit Card\"\n" | ||
| + " }") | ||
| .setResponseCode(HttpStatus.OK.value())); | ||
|
|
||
| webTestClient.get() | ||
| .uri("v2/order/1") | ||
| .exchange() | ||
| .expectStatus() | ||
| .is5xxServerError(); | ||
| } | ||
|
|
||
| @Test | ||
| void givenMockedExternalResponse_whenSearchByIdV2_thenOrderResponseShouldBeReceivedSuccessfully() { | ||
|
|
||
| mockExternalService.enqueue(new MockResponse().addHeader("Content-Type", "application/json; charset=utf-8") | ||
| .setBody("{\n" + " \"orderId\": \"a1b2c3d4-e5f6-4a5b-8c9d-0123456789ab\",\n" | ||
| + " \"orderDateTime\": \"2024-01-20T14:34:56+01:00\",\n" | ||
| + " \"address\": [\"123 Main St\", \"Apt 456\", \"Cityville\"],\n" | ||
| + " \"orderNotes\": [\"Special request: Handle with care\", \"Gift wrapping required\"]\n" + " }") | ||
| .setResponseCode(HttpStatus.OK.value())); | ||
|
|
||
| OrderResponse orderResponse = webTestClient.get() | ||
| .uri("v2/order/1") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectBody(OrderResponse.class) | ||
| .returnResult() | ||
| .getResponseBody(); | ||
| assertEquals(UUID.fromString("a1b2c3d4-e5f6-4a5b-8c9d-0123456789ab"), orderResponse.getOrderId()); | ||
| assertEquals(LocalDateTime.of(2024, 1, 20, 13, 34, 56), orderResponse.getOrderDateTime()); | ||
| assertThat(orderResponse.getAddress()).hasSize(3); | ||
| assertThat(orderResponse.getOrderNotes()).hasSize(2); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void tearDown() throws IOException { | ||
| mockExternalService.shutdown(); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.