BAEL-4687: simplified configuration

This commit is contained in:
Adina Rolea 2020-10-27 17:07:14 +02:00
parent 2a8ff6f23b
commit 0218395e89
4 changed files with 19 additions and 38 deletions

View File

@ -1,7 +1,6 @@
package com.baeldung.boot.jackson.controller; package com.baeldung.boot.jackson.controller;
import com.baeldung.boot.jackson.model.Coffee; 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.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -12,14 +11,11 @@ import java.time.LocalDateTime;
public class CoffeeController { public class CoffeeController {
@GetMapping("/coffee") @GetMapping("/coffee")
public CoffeeResponse<Coffee> createCoffee(@RequestParam(required = false) String brand, public Coffee getCoffee(@RequestParam(required = false) String brand,
@RequestParam(required = false) String name) { @RequestParam(required = false) String name) {
Coffee coffee = new Coffee() return new Coffee()
.setBrand(brand) .setBrand(brand)
.setName(name);
return new CoffeeResponse<Coffee>()
.setDate(LocalDateTime.now()) .setDate(LocalDateTime.now())
.setBody(coffee); .setName(name);
} }
} }

View File

@ -1,11 +1,15 @@
package com.baeldung.boot.jackson.model; package com.baeldung.boot.jackson.model;
import java.time.LocalDateTime;
public class Coffee { public class Coffee {
private String name; private String name;
private String brand; private String brand;
private LocalDateTime date;
public String getName() { public String getName() {
return name; return name;
} }
@ -23,4 +27,13 @@ public class Coffee {
this.brand = brand; this.brand = brand;
return this; return this;
} }
public LocalDateTime getDate() {
return date;
}
public Coffee setDate(LocalDateTime date) {
this.date = date;
return this;
}
} }

View File

@ -1,28 +0,0 @@
package com.baeldung.boot.jackson.model;
import java.time.LocalDateTime;
public class CoffeeResponse<T> {
private LocalDateTime date;
private T body;
public LocalDateTime getDate() {
return date;
}
public CoffeeResponse<T> setDate(LocalDateTime date) {
this.date = date;
return this;
}
public T getBody() {
return body;
}
public CoffeeResponse<T> setBody(T body) {
this.body = body;
return this;
}
}

View File

@ -18,7 +18,7 @@ public class CoffeeIntegrationTest {
TestRestTemplate restTemplate; TestRestTemplate restTemplate;
@Test @Test
public void whenQueryCoffeeWithoutParam_thenNullIsNotInserted() { public void whenGetCoffee_thenSerializedWithDateAndNonNull() {
String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat) String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat)
.format(LocalDateTime.now()); .format(LocalDateTime.now());
String brand = "Lavazza"; String brand = "Lavazza";
@ -27,6 +27,6 @@ public class CoffeeIntegrationTest {
String response = restTemplate.getForObject(url, String.class); String response = restTemplate.getForObject(url, String.class);
assertThat(response).isEqualTo( assertThat(response).isEqualTo(
"{\"date\":\"" + formattedDate + "\",\"body\":{\"brand\":\"" + brand + "\"}}"); "{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}");
} }
} }