From 97e9ba0c3f8ef20829223d2f1a1db06581a472cb Mon Sep 17 00:00:00 2001 From: Amit Pandey Date: Mon, 22 Jan 2024 13:30:05 +0530 Subject: [PATCH] [JAVA-30357] Upgrade spring-mvc-java-2 to Spring Boot 3 (#15625) --- spring-web-modules/spring-mvc-java-2/pom.xml | 30 ++++++------------- .../cache/CacheControlController.java | 2 +- .../com/baeldung/excel/ExcelController.java | 2 +- .../matrix/controller/CompanyController.java | 2 +- .../matrix/controller/EmployeeController.java | 4 +-- .../com/baeldung/matrix/model/Employee.java | 3 +- .../MultipartPostRequestController.java | 3 +- .../CustomWebMvcConfigurationSupport.java | 2 -- .../SiteController.java | 1 - .../com/baeldung/htmlunit/TestConfig.java | 19 +++++++----- .../matrix/EmployeeMvcIntegrationTest.java | 2 +- .../ConvertFileToMultipartFileUnitTest.java | 14 ++++----- 12 files changed, 36 insertions(+), 48 deletions(-) diff --git a/spring-web-modules/spring-mvc-java-2/pom.xml b/spring-web-modules/spring-mvc-java-2/pom.xml index a4484ed56d..f91e2dd541 100644 --- a/spring-web-modules/spring-mvc-java-2/pom.xml +++ b/spring-web-modules/spring-mvc-java-2/pom.xml @@ -10,21 +10,15 @@ com.baeldung - parent-boot-2 + parent-boot-3 0.0.1-SNAPSHOT - ../../parent-boot-2 + ../../parent-boot-3 - - javax.servlet - javax.servlet-api - ${javax.version} - org.springframework spring-webmvc - ${spring.mvc.version} com.fasterxml.jackson.core @@ -37,15 +31,10 @@ ${commons-io.version} - org.glassfish.jaxb - jaxb-runtime + com.sun.xml.bind + jaxb-impl ${jaxb-runtime.version} - - commons-fileupload - commons-fileupload - ${commons-fileupload.version} - net.sourceforge.htmlunit htmlunit @@ -80,6 +69,7 @@ javax.servlet jstl + ${jstl.version} org.apache.tomcat.embed @@ -89,7 +79,7 @@ org.thymeleaf - thymeleaf-spring4 + thymeleaf-spring5 ${thymeleaf.version} @@ -110,15 +100,13 @@ - 4.0.1 - 5.2.2.RELEASE - 2.3.5 - 1.5 + 4.0.1 2.32 3.16-beta1 3.0.1-b09 2.3.3 - 3.0.9.RELEASE + 3.1.2.RELEASE + true \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java index 7305f836cf..641611f5c9 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java @@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.context.request.WebRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletResponse; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.concurrent.TimeUnit; diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/excel/ExcelController.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/excel/ExcelController.java index 5cb5de7124..cda8ef123d 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/excel/ExcelController.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/excel/ExcelController.java @@ -7,7 +7,7 @@ import java.io.InputStream; import java.util.List; import java.util.Map; -import javax.annotation.Resource; +import jakarta.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java index 7a21ded026..81ff304047 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java @@ -49,7 +49,7 @@ public class CompanyController { @RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET) @ResponseBody public ResponseEntity> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) { - final Map result = new HashMap(); + final Map result = new HashMap<>(); result.put("name", name); return new ResponseEntity<>(result, HttpStatus.OK); } diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java index 3d5df0653d..48b58adbc2 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java @@ -53,7 +53,7 @@ public class EmployeeController { @RequestMapping(value = "/employees/{name}", method = RequestMethod.GET) @ResponseBody public ResponseEntity> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) { - final List employeesList = new ArrayList(); + final List employeesList = new ArrayList<>(); for (final Map.Entry employeeEntry : employeeMap.entrySet()) { final Employee employee = employeeEntry.getValue(); if (employee.getName().equalsIgnoreCase(name) && employee.getContactNumber().startsWith(beginContactNumber)) { @@ -66,7 +66,7 @@ public class EmployeeController { @RequestMapping(value = "/employeesContacts/{contactNumber}", method = RequestMethod.GET) @ResponseBody public ResponseEntity> getEmployeeByContactNumber(@MatrixVariable(required = true) final String contactNumber) { - final List employeesList = new ArrayList(); + final List employeesList = new ArrayList<>(); for (final Map.Entry employeeEntry : employeeMap.entrySet()) { final Employee employee = employeeEntry.getValue(); if (employee.getContactNumber().equalsIgnoreCase(contactNumber)) { diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java index c3384122b4..b6aa3815fc 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java @@ -1,6 +1,7 @@ package com.baeldung.matrix.model; -import javax.xml.bind.annotation.XmlRootElement; + +import jakarta.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Employee { diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java index d624ea368f..a9cf4c4f7a 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java @@ -12,6 +12,7 @@ public class MultipartPostRequestController { @PostMapping(path = "/upload") public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { - return file.isEmpty() ? new ResponseEntity(HttpStatus.NOT_FOUND) : new ResponseEntity(HttpStatus.OK); + return file.isEmpty() ? new ResponseEntity<>(HttpStatus.NOT_FOUND) : new ResponseEntity<>( + HttpStatus.OK); } } \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java index 24d5a447ab..d4b716d6f1 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java @@ -10,8 +10,6 @@ public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport @Override protected PathMatchConfigurer getPathMatchConfigurer() { PathMatchConfigurer pathMatchConfigurer = super.getPathMatchConfigurer(); - pathMatchConfigurer.setUseSuffixPatternMatch(false); - return pathMatchConfigurer; } } diff --git a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java index c9584afdc9..86ad9e531d 100644 --- a/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java +++ b/spring-web-modules/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java @@ -1,6 +1,5 @@ package com.baeldung.pathvariable.dottruncated; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/htmlunit/TestConfig.java b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/htmlunit/TestConfig.java index 6e55f01454..5d7264137c 100644 --- a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/htmlunit/TestConfig.java +++ b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/htmlunit/TestConfig.java @@ -1,17 +1,20 @@ package com.baeldung.htmlunit; -import javax.servlet.ServletContext; - import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.thymeleaf.spring4.SpringTemplateEngine; -import org.thymeleaf.spring4.view.ThymeleafViewResolver; -import org.thymeleaf.templateresolver.ServletContextTemplateResolver; +import org.thymeleaf.spring5.SpringTemplateEngine; +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.spring5.view.ThymeleafViewResolver; +import org.thymeleaf.templateresolver.WebApplicationTemplateResolver; +import org.thymeleaf.web.IWebApplication; +import org.thymeleaf.web.servlet.IServletWebApplication; +import org.thymeleaf.web.servlet.JakartaServletWebApplication; @Configuration @EnableWebMvc @@ -19,7 +22,7 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver; public class TestConfig implements WebMvcConfigurer { @Autowired - private ServletContext ctx; + private ApplicationContext ctx; @Bean public ViewResolver thymeleafViewResolver() { @@ -30,8 +33,8 @@ public class TestConfig implements WebMvcConfigurer { } @Bean - public ServletContextTemplateResolver templateResolver() { - final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(ctx); + public SpringResourceTemplateResolver templateResolver() { + final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver (); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); diff --git a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java index c061c1efc7..6d91f08bb0 100644 --- a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java +++ b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java @@ -29,7 +29,7 @@ public class EmployeeMvcIntegrationTest { @Before public void setup() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); } diff --git a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java index 7d99f7f9bb..47b9bfb058 100644 --- a/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java +++ b/spring-web-modules/spring-mvc-java-2/src/test/java/com/baeldung/multipart/file/ConvertFileToMultipartFileUnitTest.java @@ -4,18 +4,16 @@ import static org.junit.Assert.assertEquals; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; -import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.multipart.commons.CommonsMultipartFile; +import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; public class ConvertFileToMultipartFileUnitTest { @@ -32,11 +30,11 @@ public class ConvertFileToMultipartFileUnitTest { @Test public void givenFile_whenCreateMultipartFileUsingCommonsMultipart_thenContentMatch() throws IOException { File file = new File("src/main/resources/targetFile.tmp"); - FileItem fileItem = new DiskFileItem("file", Files.probeContentType(file.toPath()), false, file.getName(), (int) file.length(), file.getParentFile()); InputStream input = new FileInputStream(file); - OutputStream outputStream = fileItem.getOutputStream(); + byte [] arr = IOUtils.toByteArray(input); + OutputStream outputStream = new FileOutputStream(file); IOUtils.copy(input, outputStream); - MultipartFile multipartFile = new CommonsMultipartFile(fileItem); + MultipartFile multipartFile = new MockMultipartFile("test","targetFile.tmp", MediaType.TEXT_PLAIN_VALUE, arr); String fileContent = new String(multipartFile.getBytes()); assertEquals("Hello World", fileContent); assertEquals("targetFile.tmp", multipartFile.getOriginalFilename());