JAVA-32053 Upgrade spring-mvc-basics-3 to Spring Boot 3 (#16231)

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1 2024-03-30 19:42:52 +02:00 committed by GitHub
parent dbe6e67b7e
commit 6ff926e003
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 75 additions and 95 deletions

View File

@ -10,9 +10,9 @@
<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>
@ -51,18 +51,13 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${jpa.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.subethamail</groupId>
<groupId>com.github.davidmoten</groupId>
<artifactId>subethasmtp</artifactId>
<version>${subethasmtp.version}</version>
<scope>test</scope>
@ -130,7 +125,7 @@
<jquery.version>3.1.1</jquery.version>
<bootstrap.version>3.3.7-1</bootstrap.version>
<jpa.version>2.2</jpa.version>
<subethasmtp.version>3.1.7</subethasmtp.version>
<subethasmtp.version>7.0.2</subethasmtp.version>
<httpclient.version>4.5.8</httpclient.version>
</properties>

View File

@ -2,13 +2,11 @@ package com.baeldung.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
SpringApplication.run(Application.class, args);
}
}

View File

@ -53,6 +53,6 @@ public class GenericEntityController {
@GetMapping("/entity/findbyversion")
public ResponseEntity findByVersion(@Version String version) {
return version != null ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK) : new ResponseEntity(HttpStatus.NOT_FOUND);
return version != null ? new ResponseEntity<>(entityList.stream().findFirst().get(), HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

View File

@ -27,7 +27,7 @@ public class GenericBigDecimalConverter implements GenericConverter {
return new BigDecimal(number);
} else {
Number number = (Number) source;
BigDecimal converted = new BigDecimal(number.doubleValue());
BigDecimal converted = BigDecimal.valueOf(number.doubleValue());
return converted.setScale(2, BigDecimal.ROUND_HALF_EVEN);
}
}

View File

@ -1,7 +1,7 @@
package com.baeldung.boot.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Employee {

View File

@ -1,6 +1,7 @@
package com.baeldung.boot.domain;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.thymeleaf.util.StringUtils.randomAlphanumeric;
public class Foo extends AbstractEntity {
private String name;

View File

@ -1,9 +1,9 @@
package com.baeldung.boot.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class GenericEntity {

View File

@ -7,7 +7,7 @@ import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import javax.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;
@Component
public class HeaderVersionArgumentResolver implements HandlerMethodArgumentResolver {

View File

@ -6,9 +6,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.springframework.util.StreamUtils;

View File

@ -4,8 +4,8 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
public class CachedBodyServletInputStream extends ServletInputStream {

View File

@ -2,11 +2,11 @@ package com.baeldung.cachedrequest;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

View File

@ -5,10 +5,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* To initialize the WebApplication, Please see
* {@link com.baeldung.spring.config.MainWebAppInitializer}
* To initialize the WebApplication
*/
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.baeldung.cachedrequest")

View File

@ -4,19 +4,18 @@ package com.baeldung.cachedrequest;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.filter.OncePerRequestFilter;
@Order(Ordered.LOWEST_PRECEDENCE)
@Order
@Component
@WebFilter(filterName = "printRequestContentFilter", urlPatterns = "/*")
public class PrintRequestContentFilter extends OncePerRequestFilter {

View File

@ -6,8 +6,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
@Documented
@Constraint(validatedBy = ContactNumberValidator.class)

View File

@ -1,7 +1,7 @@
package com.baeldung.customvalidator;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
public class ContactNumberValidator implements ConstraintValidator<ContactNumberConstraint, String> {

View File

@ -5,8 +5,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
@Constraint(validatedBy = FieldsValueMatchValidator.class)
@Target({ ElementType.TYPE })

View File

@ -1,7 +1,7 @@
package com.baeldung.customvalidator;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.beans.BeanWrapperImpl;

View File

@ -1,6 +1,6 @@
package com.baeldung.customvalidator;
import javax.validation.Valid;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

@ -1,6 +1,6 @@
package com.baeldung.customvalidator;
import javax.validation.Valid;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

@ -11,7 +11,7 @@ import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.RedirectView;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;
@Controller
public class PoemSubmission {

View File

@ -1,14 +1,10 @@
package com.baeldung.interpolation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.MessageInterpolator;
import jakarta.validation.MessageInterpolator;
import java.util.Locale;
public class MyMessageInterpolator implements MessageInterpolator {
private static Logger logger = LoggerFactory.getLogger(MyMessageInterpolator.class);
private final MessageInterpolator defaultInterpolator;
public MyMessageInterpolator(MessageInterpolator interpolator) {

View File

@ -1,6 +1,6 @@
package com.baeldung.interpolation;
import javax.validation.constraints.NotNull;
import jakarta.validation.constraints.NotNull;
public class NotNullRequest {

View File

@ -1,6 +1,6 @@
package com.baeldung.interpolation;
import javax.validation.Valid;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

View File

@ -1,14 +1,11 @@
package com.baeldung.interpolation;
import java.util.Formatter;
import javax.validation.constraints.Size;
import javax.validation.constraints.Min;
import javax.validation.constraints.DecimalMin;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
public class ValidationExamples {
private static final Formatter formatter = new Formatter();
@Size(
min = 5,
max = 14,

View File

@ -22,7 +22,7 @@ public class ReadHeaderRestController {
@GetMapping("/")
public ResponseEntity<String> index() {
return new ResponseEntity<String>("Index", HttpStatus.OK);
return new ResponseEntity<>("Index", HttpStatus.OK);
}
@GetMapping("/greeting")
@ -46,12 +46,12 @@ public class ReadHeaderRestController {
break;
}
return new ResponseEntity<String>(greeting, HttpStatus.OK);
return new ResponseEntity<>(greeting, HttpStatus.OK);
}
@GetMapping("/double")
public ResponseEntity<String> doubleNumber(@RequestHeader("my-number") int myNumber) {
return new ResponseEntity<String>(
return new ResponseEntity<>(
String.format("%d * 2 = %d", myNumber, (myNumber * 2)),
HttpStatus.OK);
}
@ -59,34 +59,30 @@ public class ReadHeaderRestController {
@GetMapping("/listHeaders")
public ResponseEntity<String> listAllHeaders(@RequestHeader Map<String, String> headers) {
headers.forEach((key, value) -> {
LOG.info(String.format("Header '%s' = %s", key, value));
});
headers.forEach((key, value) -> LOG.info(String.format("Header '%s' = %s", key, value)));
return new ResponseEntity<String>(String.format("Listed %d headers", headers.size()), HttpStatus.OK);
return new ResponseEntity<>(String.format("Listed %d headers", headers.size()), HttpStatus.OK);
}
@GetMapping("/multiValue")
public ResponseEntity<String> multiValue(@RequestHeader MultiValueMap<String, String> headers) {
headers.forEach((key, value) -> {
LOG.info(String.format("Header '%s' = %s", key, value.stream().collect(Collectors.joining("|"))));
});
headers.forEach((key, value) -> LOG.info(String.format("Header '%s' = %s", key, String.join("|", value))));
return new ResponseEntity<String>(String.format("Listed %d headers", headers.size()), HttpStatus.OK);
return new ResponseEntity<>(String.format("Listed %d headers", headers.size()), HttpStatus.OK);
}
@GetMapping("/getBaseUrl")
public ResponseEntity<String> getBaseUrl(@RequestHeader HttpHeaders headers) {
InetSocketAddress host = headers.getHost();
String url = "http://" + host.getHostName() + ":" + host.getPort();
return new ResponseEntity<String>(String.format("Base URL = %s", url), HttpStatus.OK);
return new ResponseEntity<>(String.format("Base URL = %s", url), HttpStatus.OK);
}
@GetMapping("/nonRequiredHeader")
public ResponseEntity<String> evaluateNonRequiredHeader(
@RequestHeader(value = "optional-header", required = false) String optionalHeader) {
return new ResponseEntity<String>(
return new ResponseEntity<>(
String.format("Was the optional header present? %s!", (optionalHeader == null ? "No" : "Yes")),
HttpStatus.OK);
}
@ -95,6 +91,6 @@ public class ReadHeaderRestController {
public ResponseEntity<String> evaluateDefaultHeaderValue(
@RequestHeader(value = "optional-header", defaultValue = "3600") int optionalHeader) {
return new ResponseEntity<String>(String.format("Optional Header is %d", optionalHeader), HttpStatus.OK);
return new ResponseEntity<>(String.format("Optional Header is %d", optionalHeader), HttpStatus.OK);
}
}

View File

@ -13,7 +13,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
import javax.mail.MessagingException;
import jakarta.mail.MessagingException;
import java.io.IOException;
import java.util.List;
@ -29,20 +29,20 @@ public class SpringBootMailIntegrationTest {
private Wiser wiser;
private String userTo = "user2@localhost";
private String userFrom = "user1@localhost";
private String subject = "Test subject";
private String textMail = "Text subject mail";
private final String userTo = "user2@localhost";
private final String userFrom = "user1@localhost";
private final String subject = "Test subject";
private final String textMail = "Text subject mail";
@Before
public void setUp() throws Exception {
public void setUp() {
final int TEST_PORT = 8025;
wiser = new Wiser(TEST_PORT);
wiser = Wiser.port(TEST_PORT);
wiser.start();
}
@After
public void tearDown() throws Exception {
public void tearDown() {
wiser.stop();
}

View File

@ -3,7 +3,7 @@ package com.baeldung.cachedrequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ReadListener;
import jakarta.servlet.ReadListener;
import org.junit.After;
import org.junit.Test;
@ -54,7 +54,7 @@ public class CachedBodyServletInputStreamUnitTest extends TestCase {
}
@Test
public void testGivenServletInputStreamCreatedAndBodyRead_whenCalledIsReady_ThenTrue() throws IOException {
public void testGivenServletInputStreamCreatedAndBodyRead_whenCalledIsReady_ThenTrue() {
// Given
byte[] cachedBody = "{\"firstName\" :\"abc\",\"lastName\" : \"xyz\",\"age\" : 30\"}".getBytes();
servletInputStream = new CachedBodyServletInputStream(cachedBody);
@ -85,7 +85,7 @@ public class CachedBodyServletInputStreamUnitTest extends TestCase {
}
@Test(expected = UnsupportedOperationException.class)
public void testGivenServletInputStreamCreated_whenCalledIsRead_ThenThrowsException() throws IOException {
public void testGivenServletInputStreamCreated_whenCalledIsRead_ThenThrowsException() {
// Given
byte[] cachedBody = "{\"firstName\" :\"abc\",\"lastName\" : \"xyz\",\"age\" : 30\"}".getBytes();
servletInputStream = new CachedBodyServletInputStream(cachedBody);

View File

@ -2,8 +2,8 @@ package com.baeldung.cachedrequest;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -2,8 +2,8 @@ package com.baeldung.cachedrequest;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import org.junit.Test;
import org.junit.runner.RunWith;