From 517a399cabe4dd7a40c07a81facd599df3cbf9cd Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Tue, 28 Dec 2021 16:59:23 +0100 Subject: [PATCH] Bump to Spring Boot 2.6.2 #63 --- pom.xml | 16 ++++--- .../util/ApplicationSwaggerConfig.java | 42 +++++++++++++++++++ .../resources/application-hsqldb.properties | 6 +-- .../resources/application-mysql.properties | 6 +-- .../application-postgresql.properties | 6 +-- src/main/resources/application.properties | 9 ++-- .../rest/OwnerRestControllerTests.java | 5 ++- .../rest/PetRestControllerTests.java | 4 ++ .../rest/VisitRestControllerTests.java | 2 + .../clinicService/ApplicationTestConfig.java | 2 +- .../userService/AbstractUserServiceTests.java | 2 +- src/test/resources/application.properties | 8 ++-- 12 files changed, 81 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index 8aa89d9cc..4b4a805d4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.samples spring-petclinic-rest - 2.4.2 + 2.6.2 REST version of the Spring Petclinic sample application https://spring-petclinic.github.io/ @@ -13,22 +13,26 @@ org.springframework.boot spring-boot-starter-parent - 2.4.2 + 2.6.2 + 1.2.1.RELEASE 3.0.0 - 0.8.7 - 1.3.0 - springcommunity - 3.0.2 0.2.1 1.4.1.Final 2.3.0 + + + 0.8.7 5.2.1 3.2.0 + + + 1.3.0 + springcommunity diff --git a/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java b/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java index 18df3bd71..3c406a55d 100755 --- a/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java @@ -17,19 +17,28 @@ package org.springframework.samples.petclinic.util; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.util.ReflectionUtils; +import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; +import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; import springfox.documentation.swagger2.annotations.EnableSwagger2; +import java.lang.reflect.Field; import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; /** * Java config for Springfox swagger documentation plugin @@ -67,5 +76,38 @@ private ApiInfo getApiInfo() { "http://www.apache.org/licenses/LICENSE-2.0", Collections.emptyList()); } + /** + * Springfox workaround required by Spring Boot 2.6 + * See https://github.com/springfox/springfox/issues/346 + */ + @Bean + public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { + return new BeanPostProcessor() { + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { + customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); + } + return bean; + } + + private void customizeSpringfoxHandlerMappings(List mappings) { + mappings.removeIf(mapping -> mapping.getPatternParser() != null); + } + + @SuppressWarnings("unchecked") + private List getHandlerMappings(Object bean) { + try { + Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); + field.setAccessible(true); + return (List) field.get(bean); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + }; + } + } diff --git a/src/main/resources/application-hsqldb.properties b/src/main/resources/application-hsqldb.properties index 8af33d292..5a9156628 100644 --- a/src/main/resources/application-hsqldb.properties +++ b/src/main/resources/application-hsqldb.properties @@ -1,8 +1,8 @@ # HSQLDB config start #---------------------------------------------------------------- -spring.datasource.schema=classpath*:db/hsqldb/initDB.sql -spring.datasource.data=classpath*:db/hsqldb/populateDB.sql +spring.sql.init.schema-locations=classpath*:db/hsqldb/initDB.sql +spring.sql.init.data-locations=classpath*:db/hsqldb/populateDB.sql spring.datasource.url=jdbc:hsqldb:mem:petclinic spring.datasource.username=sa @@ -11,4 +11,4 @@ spring.jpa.database=HSQL spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect spring.jpa.hibernate.ddl-auto=none #---------------------------------------------------------------- -# HSQLDB config end \ No newline at end of file +# HSQLDB config end diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties index d429b5ac1..daffa4ab3 100644 --- a/src/main/resources/application-mysql.properties +++ b/src/main/resources/application-mysql.properties @@ -1,7 +1,7 @@ # uncomment for init database (first start) -#spring.datasource.initialization-mode=always -#spring.datasource.schema=classpath*:db/mysql/initDB.sql -#spring.datasource.data=classpath*:db/mysql/populateDB.sql +#spring.sql.init.mode=always +#spring.sql.init.schema-locations=classpath*:db/mysql/initDB.sql +#spring.sql.init.data-locations=classpath*:db/mysql/populateDB.sql # MySQL config start #---------------------------------------------------------------- diff --git a/src/main/resources/application-postgresql.properties b/src/main/resources/application-postgresql.properties index ba933e5d5..456b590e4 100644 --- a/src/main/resources/application-postgresql.properties +++ b/src/main/resources/application-postgresql.properties @@ -1,7 +1,7 @@ # uncomment for init database (first start) -#spring.datasource.initialization-mode=always -#spring.datasource.schema=classpath*:db/postgresql/initDB.sql -#spring.datasource.data=classpath*:db/postgresql/populateDB.sql +#spring.sql.init.mode=always +#spring.sql.init.schema-locations=classpath*:db/postgresql/initDB.sql +#spring.sql.init.data-locations=classpath*:db/postgresql/populateDB.sql # PostgreSQL config start #---------------------------------------------------------------- diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e3669c9bc..c558a190e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,7 +23,12 @@ spring.profiles.active=hsqldb,spring-data-jpa server.port=9966 server.servlet.context-path=/petclinic/ +# Springfox workaround required by Spring Boot 2.6 +# See https://github.com/springfox/springfox/issues/3462 +spring.mvc.pathmatch.matching-strategy=ant_path_matcher + spring.messages.basename=messages/messages +spring.jpa.open-in-view=false logging.level.org.springframework=INFO #logging.level.org.springframework=DEBUG @@ -35,7 +40,3 @@ logging.level.org.springframework=INFO # by default the authentication is disabled petclinic.security.enable=false -# ------------------------------------------------ -# Spring doc configuration -springdoc.api-docs.enabled=true -springdoc.writer-with-default-pretty-printer= true diff --git a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java index b329ebe07..033d4e562 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java @@ -76,7 +76,7 @@ void initOwners() { this.mockMvc = MockMvcBuilders.standaloneSetup(ownerRestController) .setControllerAdvice(new ExceptionControllerAdvice()) .build(); - owners = new ArrayList(); + owners = new ArrayList<>(); OwnerDto ownerWithPet = new OwnerDto(); owners.add(ownerWithPet.id(1).firstName("George").lastName("Franklin").address("110 W. Liberty St.").city("Madison").telephone("6085551023").addPetsItem(getTestPetWithIdAndName(ownerWithPet, 1, "Rosy"))); @@ -304,6 +304,7 @@ void testUpdateOwnerError() throws Exception { OwnerDto newOwnerDto = owners.get(0); newOwnerDto.setFirstName(""); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); this.mockMvc.perform(put("/api/owners/1") .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) @@ -315,6 +316,7 @@ void testUpdateOwnerError() throws Exception { void testDeleteOwnerSuccess() throws Exception { OwnerDto newOwnerDto = owners.get(0); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); final Owner owner = ownerMapper.toOwner(owners.get(0)); given(this.clinicService.findOwnerById(1)).willReturn(owner); @@ -328,6 +330,7 @@ void testDeleteOwnerSuccess() throws Exception { void testDeleteOwnerError() throws Exception { OwnerDto newOwnerDto = owners.get(0); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); given(this.clinicService.findOwnerById(-1)).willReturn(null); this.mockMvc.perform(delete("/api/owners/-1") diff --git a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java index 2297d1128..7778f271e 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java @@ -176,6 +176,7 @@ void testCreatePetError() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + mapper.registerModule(new JavaTimeModule()); String newPetAsJSON = mapper.writeValueAsString(newPet); this.mockMvc.perform(post("/api/pets/") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) @@ -214,6 +215,7 @@ void testUpdatePetError() throws Exception { PetDto newPet = pets.get(0); newPet.setName(null); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newPetAsJSON = mapper.writeValueAsString(newPet); @@ -228,6 +230,7 @@ void testUpdatePetError() throws Exception { void testDeletePetSuccess() throws Exception { PetDto newPet = pets.get(0); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newPetAsJSON = mapper.writeValueAsString(newPet); given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); this.mockMvc.perform(delete("/api/pets/3") @@ -240,6 +243,7 @@ void testDeletePetSuccess() throws Exception { void testDeletePetError() throws Exception { PetDto newPet = pets.get(0); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newPetAsJSON = mapper.writeValueAsString(newPet); given(this.clinicService.findPetById(-1)).willReturn(null); this.mockMvc.perform(delete("/api/pets/-1") diff --git a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java index 33cad0564..7a9afdc9e 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java @@ -225,6 +225,7 @@ void testUpdateVisitError() throws Exception { void testDeleteVisitSuccess() throws Exception { Visit newVisit = visits.get(0); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit)); given(this.clinicService.findVisitById(2)).willReturn(visits.get(0)); this.mockMvc.perform(delete("/api/visits/2") @@ -237,6 +238,7 @@ void testDeleteVisitSuccess() throws Exception { void testDeleteVisitError() throws Exception { Visit newVisit = visits.get(0); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit)); given(this.clinicService.findVisitById(-1)).willReturn(null); this.mockMvc.perform(delete("/api/visits/-1") diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java index a20145521..79398dc1e 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java +++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java @@ -7,7 +7,7 @@ public class ApplicationTestConfig { public ApplicationTestConfig(){ - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); } } diff --git a/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java index 488ce61ae..54ecdded4 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java @@ -17,7 +17,7 @@ public abstract class AbstractUserServiceTests { @BeforeEach public void init() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); } @Test diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 09ce9a4db..31f3b8080 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -21,7 +21,8 @@ spring.profiles.active=hsqldb,spring-data-jpa # ------------------------------------------------ server.port=9966 -server.context-path=/petclinic/ +server.servlet.context-path=/petclinic/ +spring.jpa.open-in-view=false spring.messages.basename=messages/messages logging.level.org.springframework=INFO @@ -35,7 +36,4 @@ logging.level.org.springframework=INFO security.ignored=/** basic.authentication.enabled=true petclinic.security.enable=true -# ------------------------------------------------ -# Spring doc configuration -springdoc.api-docs.enabled=true -springdoc.writer-with-default-pretty-printer=true +