[JAVA-30357] Upgrade spring-mvc-java-2 to Spring Boot 3 (#15625)

This commit is contained in:
Amit Pandey 2024-01-22 13:30:05 +05:30 committed by GitHub
parent 22a1ca2978
commit 97e9ba0c3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 36 additions and 48 deletions

View File

@ -10,21 +10,15 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.mvc.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
@ -37,15 +31,10 @@
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb-runtime.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
@ -80,6 +69,7 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
@ -89,7 +79,7 @@
<!-- Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<artifactId>thymeleaf-spring5</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
@ -110,15 +100,13 @@
</build>
<properties>
<javax.version>4.0.1</javax.version>
<spring.mvc.version>5.2.2.RELEASE</spring.mvc.version>
<jaxb-runtime.version>2.3.5</jaxb-runtime.version>
<commons-fileupload.version>1.5</commons-fileupload.version>
<jaxb-runtime.version>4.0.1</jaxb-runtime.version>
<htmlunit.version>2.32</htmlunit.version>
<poi.version>3.16-beta1</poi.version>
<javax.el.version>3.0.1-b09</javax.el.version>
<javax-servlet-api.version>2.3.3</javax-servlet-api.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf.version>3.1.2.RELEASE</thymeleaf.version>
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>
</project>

View File

@ -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;

View File

@ -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;

View File

@ -49,7 +49,7 @@ public class CompanyController {
@RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Map<String, String>> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) {
final Map<String, String> result = new HashMap<String, String>();
final Map<String, String> result = new HashMap<>();
result.put("name", name);
return new ResponseEntity<>(result, HttpStatus.OK);
}

View File

@ -53,7 +53,7 @@ public class EmployeeController {
@RequestMapping(value = "/employees/{name}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<List<Employee>> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) {
final List<Employee> employeesList = new ArrayList<Employee>();
final List<Employee> employeesList = new ArrayList<>();
for (final Map.Entry<Long, Employee> 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<List<Employee>> getEmployeeByContactNumber(@MatrixVariable(required = true) final String contactNumber) {
final List<Employee> employeesList = new ArrayList<Employee>();
final List<Employee> employeesList = new ArrayList<>();
for (final Map.Entry<Long, Employee> employeeEntry : employeeMap.entrySet()) {
final Employee employee = employeeEntry.getValue();
if (employee.getContactNumber().equalsIgnoreCase(contactNumber)) {

View File

@ -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 {

View File

@ -12,6 +12,7 @@ public class MultipartPostRequestController {
@PostMapping(path = "/upload")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
return file.isEmpty() ? new ResponseEntity<String>(HttpStatus.NOT_FOUND) : new ResponseEntity<String>(HttpStatus.OK);
return file.isEmpty() ? new ResponseEntity<>(HttpStatus.NOT_FOUND) : new ResponseEntity<>(
HttpStatus.OK);
}
}

View File

@ -10,8 +10,6 @@ public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport
@Override
protected PathMatchConfigurer getPathMatchConfigurer() {
PathMatchConfigurer pathMatchConfigurer = super.getPathMatchConfigurer();
pathMatchConfigurer.setUseSuffixPatternMatch(false);
return pathMatchConfigurer;
}
}

View File

@ -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;

View File

@ -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");

View File

@ -29,7 +29,7 @@ public class EmployeeMvcIntegrationTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}

View File

@ -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());