From 059a9e6a51d065cca716a6f57d48061b136c0887 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 19 Feb 2020 22:36:27 +0100 Subject: [PATCH] BAEL-3801: Update spring-boot-rest to use latest Spring Boot version (#8740) * BAEL-3801: Upgrade to Spring Boot 2.2.2 * BAEL-3801: Fix JacksonMarshaller * BAEL-3801: Get rid of deprecated HATEOAS classes * BAEL-3801: Fix H2 tables drop order * BAEL-3801: Remove commented property from pom.xml --- spring-boot-rest/pom.xml | 1 - .../persistence/config/CustomH2Dialect.java | 26 +++++++++++++++++++ .../baeldung/persistence/model/Customer.java | 9 +++---- .../com/baeldung/persistence/model/Order.java | 4 +-- .../web/controller/CustomerController.java | 14 +++++----- .../main/resources/persistence-h2.properties | 2 +- .../com/baeldung/test/JacksonMarshaller.java | 2 +- 7 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java diff --git a/spring-boot-rest/pom.xml b/spring-boot-rest/pom.xml index 2483aab6be..10dacf99e8 100644 --- a/spring-boot-rest/pom.xml +++ b/spring-boot-rest/pom.xml @@ -95,7 +95,6 @@ 27.0.1-jre 1.4.11.1 2.3.5 - 2.1.9.RELEASE diff --git a/spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java b/spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java new file mode 100644 index 0000000000..0108f92b2c --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java @@ -0,0 +1,26 @@ +package com.baeldung.persistence.config; + +import org.hibernate.dialect.H2Dialect; + +/** + * Since H2 1.4.200. the behavior of the drop table commands has changed. + * Tables are not dropped in a correct order. + * Until this is properly fixed directly in Hibernate project, + * let's use this custom H2Dialect class to solve this issue. + * + * @see https://hibernate.atlassian.net/browse/HHH-13711 + * @see https://github.com/hibernate/hibernate-orm/pull/3093 + */ +public class CustomH2Dialect extends H2Dialect { + + @Override + public boolean dropConstraints() { + return true; + } + + @Override + public boolean supportsIfExistsAfterAlterTable() { + return true; + } + +} diff --git a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java index 10da2e10f0..06b2485c7b 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java +++ b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java @@ -1,14 +1,13 @@ package com.baeldung.persistence.model; -import java.util.Map; - -import org.springframework.hateoas.ResourceSupport; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; +import org.springframework.hateoas.RepresentationModel; + +import java.util.Map; @JsonInclude(Include.NON_NULL) -public class Customer extends ResourceSupport { +public class Customer extends RepresentationModel { private String customerId; private String customerName; private String companyName; diff --git a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java index 7aea9bce5c..b5e1a2cee2 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java +++ b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java @@ -1,8 +1,8 @@ package com.baeldung.persistence.model; -import org.springframework.hateoas.ResourceSupport; +import org.springframework.hateoas.RepresentationModel; -public class Order extends ResourceSupport { +public class Order extends RepresentationModel { private String orderId; private double price; private int quantity; diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java b/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java index 91aa9f2144..2b7dc1eee1 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java +++ b/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java @@ -1,13 +1,13 @@ package com.baeldung.web.controller; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Link; -import org.springframework.hateoas.Resources; +import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; import org.springframework.web.bind.annotation.GetMapping; @@ -41,7 +41,7 @@ public class CustomerController { } @GetMapping(value = "/{customerId}/orders", produces = { "application/hal+json" }) - public Resources getOrdersForCustomer(@PathVariable final String customerId) { + public CollectionModel getOrdersForCustomer(@PathVariable final String customerId) { final List orders = orderService.getAllOrdersForCustomer(customerId); for (final Order order : orders) { final Link selfLink = linkTo( @@ -50,12 +50,12 @@ public class CustomerController { } Link link = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel(); - Resources result = new Resources<>(orders, link); + CollectionModel result = new CollectionModel<>(orders, link); return result; } @GetMapping(produces = { "application/hal+json" }) - public Resources getAllCustomers() { + public CollectionModel getAllCustomers() { final List allCustomers = customerService.allCustomers(); for (final Customer customer : allCustomers) { @@ -72,7 +72,7 @@ public class CustomerController { } Link link = linkTo(CustomerController.class).withSelfRel(); - Resources result = new Resources<>(allCustomers, link); + CollectionModel result = new CollectionModel<>(allCustomers, link); return result; } diff --git a/spring-boot-rest/src/main/resources/persistence-h2.properties b/spring-boot-rest/src/main/resources/persistence-h2.properties index 839a466533..efbf3f7db7 100644 --- a/spring-boot-rest/src/main/resources/persistence-h2.properties +++ b/spring-boot-rest/src/main/resources/persistence-h2.properties @@ -17,6 +17,6 @@ jdbc.user=sa jdbc.pass= # hibernate.X -hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.dialect=com.baeldung.persistence.config.CustomH2Dialect hibernate.show_sql=false hibernate.hbm2ddl.auto=create-drop diff --git a/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java b/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java index 23b5d60b6b..0f89e0a9de 100644 --- a/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java +++ b/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java @@ -60,7 +60,7 @@ public final class JacksonMarshaller implements IMarshaller { List entities = null; try { if (clazz.equals(Foo.class)) { - entities = objectMapper.readValue(resourcesAsString, new TypeReference>() { + entities = objectMapper.readValue(resourcesAsString, new TypeReference>() { // ... }); } else {