From 8bd2ca68939548fbb31d918ae105d6887261e2c8 Mon Sep 17 00:00:00 2001 From: Amit Pandey Date: Sat, 23 Mar 2024 17:19:34 +0530 Subject: [PATCH] JAVA-31602 :- Upgrade spring-boot-mvc-2, spring-boot-mvc-5 to boot 3.2.2 (#16188) * JAVA-31602 :- Upgrade spring-boot-mvc-2, spring-boot-mvc-5 to boot 3.2.2 * JAVA-31602 :- Upgrade spring-boot-mvc-2, spring-boot-mvc-5 to boot 3.2.2 * Revert parent pom to restrict build * JAVA-31602 :- Upgrade spring-boot-mvc-2, spring-boot-mvc-5 to boot 3.2.2 * Fix version error * JAVA-31602 :- Upgrade spring-boot-mvc-2, spring-boot-mvc-5 to boot 3.2.2 * Fix version error * JAVA-31602 :- Upgrade spring-boot-mvc-2, spring-boot-mvc-5 to boot 3.2.2 * Fix version error * Fix version error * Enable in Parent POM * Fix haslength approach --------- Co-authored-by: Amit Pandey --- spring-boot-modules/pom.xml | 2 +- spring-boot-modules/spring-boot-mvc-2/pom.xml | 5 +++++ .../java/com/baeldung/mime/FooController.java | 3 --- .../springbootmvc/ctrl/ProductController.java | 15 +++++---------- .../springbootmvc/svc/ProductService.java | 2 +- .../com/baeldung/students/StudentService.java | 2 +- spring-boot-modules/spring-boot-mvc-5/pom.xml | 13 ++++--------- .../requestwrapper/EscapeHtmlRequestWrapper.java | 11 +++++++++++ .../EscapeHtmlAspectIntegrationTest.java | 2 ++ .../EscapeHtmlFilterIntegrationTest.java | 5 ++++- 10 files changed, 34 insertions(+), 26 deletions(-) diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 3be66dc168..873232123b 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -58,7 +58,7 @@ spring-boot-mvc-2 spring-boot-mvc-3 spring-boot-mvc-4 - + spring-boot-mvc-5 spring-boot-mvc-birt spring-boot-mvc-jersey spring-boot-nashorn diff --git a/spring-boot-modules/spring-boot-mvc-2/pom.xml b/spring-boot-modules/spring-boot-mvc-2/pom.xml index bdf9b66a86..b6ac5bdcd7 100644 --- a/spring-boot-modules/spring-boot-mvc-2/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-2/pom.xml @@ -53,6 +53,7 @@ io.rest-assured rest-assured test + ${rest-assured-version} @@ -80,7 +81,11 @@ 3.0.0 com.baeldung.springbootmvc.SpringBootMvcFnApplication + 17 1.4.11.1 + 5.4.0 + 3.2.2 + 5.10.2 \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/mime/FooController.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/mime/FooController.java index b5c0bdad12..c576c3cb1b 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/mime/FooController.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/mime/FooController.java @@ -2,12 +2,9 @@ package com.baeldung.mime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/ctrl/ProductController.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/ctrl/ProductController.java index 6a77e72cea..351dd2344f 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/ctrl/ProductController.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/ctrl/ProductController.java @@ -4,8 +4,9 @@ import static org.springframework.web.servlet.function.RouterFunctions.route; import static org.springframework.web.servlet.function.ServerResponse.ok; import static org.springframework.web.servlet.function.ServerResponse.status; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.baeldung.springbootmvc.SpringBootMvcFnApplication.Error; +import com.baeldung.springbootmvc.model.Product; +import com.baeldung.springbootmvc.svc.ProductService; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.servlet.function.EntityResponse; @@ -14,10 +15,6 @@ import org.springframework.web.servlet.function.RouterFunction; import org.springframework.web.servlet.function.ServerRequest; import org.springframework.web.servlet.function.ServerResponse; -import com.baeldung.springbootmvc.SpringBootMvcFnApplication.Error; -import com.baeldung.springbootmvc.model.Product; -import com.baeldung.springbootmvc.svc.ProductService; - @Component public class ProductController { @@ -27,10 +24,8 @@ public class ProductController { } public RouterFunction productSearch(ProductService ps) { - return route().nest(RequestPredicates.path("/product"), builder -> { - builder.GET("/name/{name}", req -> ok().body(ps.findByName(req.pathVariable("name")))) - .GET("/id/{id}", req -> ok().body(ps.findById(Integer.parseInt(req.pathVariable("id"))))); - }) + return route().nest(RequestPredicates.path("/product"), builder -> builder.GET("/name/{name}", req -> ok().body(ps.findByName(req.pathVariable("name")))) + .GET("/id/{id}", req -> ok().body(ps.findById(Integer.parseInt(req.pathVariable("id")))))) .onError(ProductService.ItemNotFoundException.class, (e, req) -> EntityResponse.fromObject(new Error(e.getMessage())) .status(HttpStatus.NOT_FOUND) .build()) diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/svc/ProductService.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/svc/ProductService.java index e2d281d54f..5be5f67a37 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/svc/ProductService.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/springbootmvc/svc/ProductService.java @@ -38,7 +38,7 @@ public class ProductService { } public Product save(Product product) { - if (StringUtils.isEmpty(product.getName()) || product.getPrice() == 0.0) { + if (!StringUtils.hasLength(product.getName()) || product.getPrice() == 0.0) { throw new IllegalArgumentException(); } int newId = products.stream() diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/students/StudentService.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/students/StudentService.java index 80f6dfd514..f42c61077c 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/students/StudentService.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/students/StudentService.java @@ -19,7 +19,7 @@ public class StudentService { new Student(2, "Sebastian","Bach"), new Student(3, "Pablo","Picasso"), }).stream() - .collect(Collectors.toConcurrentMap(s -> s.getId(), Function.identity())); + .collect(Collectors.toConcurrentMap(Student::getId, Function.identity())); // DB id sequence mock private AtomicLong sequence = new AtomicLong(3); diff --git a/spring-boot-modules/spring-boot-mvc-5/pom.xml b/spring-boot-modules/spring-boot-mvc-5/pom.xml index 10a58a6a59..3a45d58831 100644 --- a/spring-boot-modules/spring-boot-mvc-5/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-5/pom.xml @@ -65,22 +65,17 @@ JAR - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - + 3.2.2 + 5.10.2 3.0.0 com.baeldung.springboot.swagger.ArticleApplication - 2021.0.5 + 2023.0.0 1.10 + 17 \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/modifyrequest/requestwrapper/EscapeHtmlRequestWrapper.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/modifyrequest/requestwrapper/EscapeHtmlRequestWrapper.java index 66fce3c3e2..1d65b73e8e 100644 --- a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/modifyrequest/requestwrapper/EscapeHtmlRequestWrapper.java +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/modifyrequest/requestwrapper/EscapeHtmlRequestWrapper.java @@ -5,6 +5,9 @@ import jakarta.servlet.ServletInputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequestWrapper; import java.io.*; +import java.util.Collections; +import java.util.Enumeration; +import org.springframework.http.HttpHeaders; public class EscapeHtmlRequestWrapper extends HttpServletRequestWrapper { private String body = null; @@ -64,4 +67,12 @@ public class EscapeHtmlRequestWrapper extends HttpServletRequestWrapper { public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(this.getInputStream())); } + + @Override + public Enumeration getHeaders(String name) { + if(HttpHeaders.CONTENT_LENGTH.equals(name)) { + return Collections.enumeration(Collections.singletonList(String.valueOf(body.length()))); + } + return super.getHeaders(name); + } } diff --git a/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlAspectIntegrationTest.java b/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlAspectIntegrationTest.java index ef18591ccb..fe212bf6b1 100644 --- a/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlAspectIntegrationTest.java +++ b/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlAspectIntegrationTest.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -45,6 +46,7 @@ public class EscapeHtmlAspectIntegrationTest { mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save")) .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.CONTENT_LENGTH, 100) .content(objectMapper.writeValueAsString(requestBody))) .andExpect(MockMvcResultMatchers.status() .isCreated()) diff --git a/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlFilterIntegrationTest.java b/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlFilterIntegrationTest.java index c813827422..ff37cb6217 100644 --- a/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlFilterIntegrationTest.java +++ b/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/modifyrequest/EscapeHtmlFilterIntegrationTest.java @@ -1,5 +1,6 @@ package com.baeldung.modifyrequest; +import com.baeldung.modifyrequest.config.WebMvcConfiguration; import com.baeldung.modifyrequest.controller.UserController; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; @@ -9,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -21,7 +23,7 @@ import java.util.Map; @ExtendWith(SpringExtension.class) @AutoConfigureMockMvc -@WebMvcTest(UserController.class) +@WebMvcTest(value ={UserController.class, WebMvcConfiguration.class}) @ActiveProfiles("filterExample") public class EscapeHtmlFilterIntegrationTest { Logger logger = LoggerFactory.getLogger(EscapeHtmlFilterIntegrationTest.class); @@ -44,6 +46,7 @@ public class EscapeHtmlFilterIntegrationTest { mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save")) .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.CONTENT_LENGTH, "100") .content(objectMapper.writeValueAsString(requestBody))).andExpect(MockMvcResultMatchers.status() .isCreated()).andExpect(MockMvcResultMatchers.content() .json(objectMapper.writeValueAsString(expectedResponseBody)));