JAVA-32061 Upgrade spring-rest-simple to Spring Boot 3 (#16160)

This commit is contained in:
sam-gardner 2024-03-22 17:56:27 +00:00 committed by GitHub
parent 846396c6f7
commit da0dec8e13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 43 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>
@ -59,26 +59,16 @@
<version>${spring-oxm.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-jakarta</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!-- web -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>runtime</scope>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-jakarta-servlet6</artifactId>
<version>${commons-fileupload2-jakarta-servlet6.version}</version>
</dependency>
<!-- marshalling -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
@ -88,6 +78,11 @@
<artifactId>xstream</artifactId>
<version>${xstream.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jakarta.xml.bind-api.version}</version>
</dependency>
<!-- util -->
<dependency>
<groupId>com.google.guava</groupId>
@ -110,9 +105,10 @@
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${jayway-rest-assured.version}</version>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
@ -273,7 +269,6 @@
<protobuf-java-format.version>1.4</protobuf-java-format.version>
<protobuf-java.version>3.1.0</protobuf-java.version>
<xstream.version>1.4.9</xstream.version>
<jayway-rest-assured.version>2.9.0</jayway-rest-assured.version>
<spring-oxm.version>6.1.4</spring-oxm.version>
<!-- Maven plugins -->
<cargo-maven2-plugin.version>1.6.0</cargo-maven2-plugin.version>
@ -281,6 +276,11 @@
<!-- okhttp -->
<com.squareup.okhttp3.version>4.12.0</com.squareup.okhttp3.version>
<json.path.version>2.2.0</json.path.version>
<commons-fileupload.version>2.0.0-M1</commons-fileupload.version>
<rest-assured.version>5.4.0</rest-assured.version>
<jakarta.xml.bind-api.version>2.3.2</jakarta.xml.bind-api.version>
<commons-fileupload2-jakarta-servlet6.version>2.0.0-M2</commons-fileupload2-jakarta-servlet6.version>
<httpclient5.version>5.3.1</httpclient5.version>
</properties>
</project>

View File

@ -1,39 +1,36 @@
package com.baeldung.apachefileupload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.core.FileItemInput;
import org.apache.commons.fileupload2.core.FileItemInputIterator;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
@RestController
public class UploadController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(HttpServletRequest request) {
System.out.println(System.getProperty("java.io.tmpdir"));
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
boolean isMultipart = JakartaServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
factory.setFileCleaningTracker(null);
DiskFileItemFactory factory = DiskFileItemFactory.builder().get();
// Configure a repository (to ensure a secure temp location is used)
ServletFileUpload upload = new ServletFileUpload(factory);
JakartaServletFileUpload upload = new JakartaServletFileUpload(factory);
try {
// Parse the request
List<FileItem> items = upload.parseRequest(request);
@ -51,21 +48,21 @@ public class UploadController {
}
}
// Parse the request with Streaming API
upload = new ServletFileUpload();
FileItemIterator iterStream = upload.getItemIterator(request);
upload = new JakartaServletFileUpload();
FileItemInputIterator iterStream = upload.getItemIterator(request);
while (iterStream.hasNext()) {
FileItemStream item = iterStream.next();
FileItemInput item = iterStream.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
InputStream stream = item.getInputStream();
if (!item.isFormField()) {
//Process the InputStream
} else {
//process form fields
String formFieldValue = Streams.asString(stream);
String formFieldValue = IOUtils.toString(stream, StandardCharsets.UTF_8);
}
}
return "success!";
} catch (IOException | FileUploadException ex) {
} catch (IOException ex) {
return "failed: " + ex.getMessage();
}
}

View File

@ -1,6 +1,6 @@
package com.baeldung.web.util;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
/**
* Provides some constants and utility methods to build a Link Header to be stored in the {@link HttpServletResponse} object

View File

@ -5,7 +5,7 @@ import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
import com.jayway.restassured.RestAssured;
import io.restassured.RestAssured;
public class RequestMappingLiveTest {
private static String BASE_URI = "http://localhost:8082/spring-rest/ex/";