[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
12 changed files with 36 additions and 48 deletions
@@ -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;
@@ -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;
@@ -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);
}
@@ -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)) {
@@ -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 {
@@ -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);
}
}
@@ -10,8 +10,6 @@ public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport
@Override
protected PathMatchConfigurer getPathMatchConfigurer() {
PathMatchConfigurer pathMatchConfigurer = super.getPathMatchConfigurer();
pathMatchConfigurer.setUseSuffixPatternMatch(false);
return pathMatchConfigurer;
}
}
@@ -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;
@@ -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");
@@ -29,7 +29,7 @@ public class EmployeeMvcIntegrationTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@@ -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());