From 0218395e89cdc42fcd1e522502a01d2278a50636 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Tue, 27 Oct 2020 17:07:14 +0200 Subject: [PATCH] BAEL-4687: simplified configuration --- .../jackson/controller/CoffeeController.java | 12 +++----- .../baeldung/boot/jackson/model/Coffee.java | 13 +++++++++ .../boot/jackson/model/CoffeeResponse.java | 28 ------------------- .../boot/jackson/CoffeeIntegrationTest.java | 4 +-- 4 files changed, 19 insertions(+), 38 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java index 2a0f6e1240..075126de67 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java @@ -1,7 +1,6 @@ package com.baeldung.boot.jackson.controller; import com.baeldung.boot.jackson.model.Coffee; -import com.baeldung.boot.jackson.model.CoffeeResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -12,14 +11,11 @@ import java.time.LocalDateTime; public class CoffeeController { @GetMapping("/coffee") - public CoffeeResponse createCoffee(@RequestParam(required = false) String brand, - @RequestParam(required = false) String name) { - Coffee coffee = new Coffee() + public Coffee getCoffee(@RequestParam(required = false) String brand, + @RequestParam(required = false) String name) { + return new Coffee() .setBrand(brand) - .setName(name); - - return new CoffeeResponse() .setDate(LocalDateTime.now()) - .setBody(coffee); + .setName(name); } } diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java index 12c2e200df..4df6b4bd6d 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java @@ -1,11 +1,15 @@ package com.baeldung.boot.jackson.model; +import java.time.LocalDateTime; + public class Coffee { private String name; private String brand; + private LocalDateTime date; + public String getName() { return name; } @@ -23,4 +27,13 @@ public class Coffee { this.brand = brand; return this; } + + public LocalDateTime getDate() { + return date; + } + + public Coffee setDate(LocalDateTime date) { + this.date = date; + return this; + } } diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java deleted file mode 100644 index c2667df03a..0000000000 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.boot.jackson.model; - -import java.time.LocalDateTime; - -public class CoffeeResponse { - - private LocalDateTime date; - - private T body; - - public LocalDateTime getDate() { - return date; - } - - public CoffeeResponse setDate(LocalDateTime date) { - this.date = date; - return this; - } - - public T getBody() { - return body; - } - - public CoffeeResponse setBody(T body) { - this.body = body; - return this; - } -} diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java index d508ee45d1..1fda173d37 100644 --- a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java @@ -18,7 +18,7 @@ public class CoffeeIntegrationTest { TestRestTemplate restTemplate; @Test - public void whenQueryCoffeeWithoutParam_thenNullIsNotInserted() { + public void whenGetCoffee_thenSerializedWithDateAndNonNull() { String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat) .format(LocalDateTime.now()); String brand = "Lavazza"; @@ -27,6 +27,6 @@ public class CoffeeIntegrationTest { String response = restTemplate.getForObject(url, String.class); assertThat(response).isEqualTo( - "{\"date\":\"" + formattedDate + "\",\"body\":{\"brand\":\"" + brand + "\"}}"); + "{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}"); } }