From 67cf33a91bbc9a4e8536651f22406b2aa16dcf54 Mon Sep 17 00:00:00 2001 From: micropatel Date: Tue, 17 Jul 2018 23:00:02 -0300 Subject: [PATCH 1/5] Added spring-rest-template --- spring-rest-template/.gitignore | 14 ++++ spring-rest-template/README.md | 26 ++++++ spring-rest-template/checkstyle.xml | 11 +++ spring-rest-template/pom.xml | 83 +++++++++++++++++++ .../client/MultipartFileUploadClient.java | 61 ++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 spring-rest-template/.gitignore create mode 100644 spring-rest-template/README.md create mode 100644 spring-rest-template/checkstyle.xml create mode 100644 spring-rest-template/pom.xml create mode 100644 spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java diff --git a/spring-rest-template/.gitignore b/spring-rest-template/.gitignore new file mode 100644 index 0000000000..afdfaa6ded --- /dev/null +++ b/spring-rest-template/.gitignore @@ -0,0 +1,14 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* +*/.idea/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-rest-template/README.md b/spring-rest-template/README.md new file mode 100644 index 0000000000..a1d803f325 --- /dev/null +++ b/spring-rest-template/README.md @@ -0,0 +1,26 @@ +## Spring REST Templat Example Project + +### The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: +- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping) +- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest) +- [Redirect in Spring](http://www.baeldung.com/spring-redirect-and-forward) +- [Returning Custom Status Codes from Spring Controllers](http://www.baeldung.com/spring-mvc-controller-custom-http-status-code) +- [A Guide to OkHttp](http://www.baeldung.com/guide-to-okhttp) +- [Binary Data Formats in a Spring REST API](http://www.baeldung.com/spring-rest-api-with-binary-data-formats) +- [Guide to UriComponentsBuilder in Spring](http://www.baeldung.com/spring-uricomponentsbuilder) +- [Introduction to FindBugs](http://www.baeldung.com/intro-to-findbugs) +- [A Custom Media Type for a Spring REST API](http://www.baeldung.com/spring-rest-custom-media-type) +- [HTTP PUT vs HTTP PATCH in a REST API](http://www.baeldung.com/http-put-patch-difference-spring) +- [Exploring the Spring Boot TestRestTemplate](http://www.baeldung.com/spring-boot-testresttemplate) +- [Spring – Log Incoming Requests](http://www.baeldung.com/spring-http-logging) +- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations) +- [Introduction to CheckStyle](http://www.baeldung.com/checkstyle-java) +- [How to Change the Default Port in Spring Boot](http://www.baeldung.com/spring-boot-change-port) +- [Guide to DeferredResult in Spring](http://www.baeldung.com/spring-deferred-result) +- [Spring Custom Property Editor](http://www.baeldung.com/spring-mvc-custom-property-editor) +- [Using the Spring RestTemplate Interceptor](http://www.baeldung.com/spring-rest-template-interceptor) +- [Configure a RestTemplate with RestTemplateBuilder](http://www.baeldung.com/spring-rest-template-builder) + diff --git a/spring-rest-template/checkstyle.xml b/spring-rest-template/checkstyle.xml new file mode 100644 index 0000000000..85063a7570 --- /dev/null +++ b/spring-rest-template/checkstyle.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/spring-rest-template/pom.xml b/spring-rest-template/pom.xml new file mode 100644 index 0000000000..fa93308cf5 --- /dev/null +++ b/spring-rest-template/pom.xml @@ -0,0 +1,83 @@ + + 4.0.0 + com.baeldung + spring-rest-template + 0.1-SNAPSHOT + spring-rest-template + jar + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + + + org.springframework + spring-web + + + commons-logging + commons-logging + + + + + + + spring-rest-template + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${checkstyle-maven-plugin.version} + + checkstyle.xml + + + + + check + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + **/*LiveTest.java + + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${checkstyle-maven-plugin.version} + + checkstyle.xml + + + + + + + + 3.0.0 + + diff --git a/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java new file mode 100644 index 0000000000..547aec17a0 --- /dev/null +++ b/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -0,0 +1,61 @@ +package com.baeldung.web.upload.client; + +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class MultipartFileUploadClient { + + public static void main(String[] args) throws IOException { + uploadSingleFile(); + uploadMultipleFile(); + } + + private static void uploadSingleFile() throws IOException { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", getTestFile()); + + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/"; + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); + System.out.println("Response code: " + response.getStatusCode()); + } + + private static void uploadMultipleFile() throws IOException { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("files", getTestFile()); + body.add("files", getTestFile()); + body.add("files", getTestFile()); + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/"; + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); + System.out.println("Response code: " + response.getStatusCode()); + } + + public static Resource getTestFile() throws IOException { + Path testFile = Files.createTempFile("test-file", ".txt"); + System.out.println("Creating and Uploading Test File: " + testFile); + Files.write(testFile, "Hello World !!, This is a test file.".getBytes()); + return new FileSystemResource(testFile.toFile()); + } +} From cbaabf865fe062c585d492b3db63b2876627f71f Mon Sep 17 00:00:00 2001 From: micropatel Date: Wed, 18 Jul 2018 00:52:09 -0300 Subject: [PATCH 2/5] Updated README.md file --- spring-rest-template/README.md | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/spring-rest-template/README.md b/spring-rest-template/README.md index a1d803f325..698949c142 100644 --- a/spring-rest-template/README.md +++ b/spring-rest-template/README.md @@ -4,23 +4,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping) -- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest) -- [Redirect in Spring](http://www.baeldung.com/spring-redirect-and-forward) -- [Returning Custom Status Codes from Spring Controllers](http://www.baeldung.com/spring-mvc-controller-custom-http-status-code) -- [A Guide to OkHttp](http://www.baeldung.com/guide-to-okhttp) -- [Binary Data Formats in a Spring REST API](http://www.baeldung.com/spring-rest-api-with-binary-data-formats) -- [Guide to UriComponentsBuilder in Spring](http://www.baeldung.com/spring-uricomponentsbuilder) -- [Introduction to FindBugs](http://www.baeldung.com/intro-to-findbugs) -- [A Custom Media Type for a Spring REST API](http://www.baeldung.com/spring-rest-custom-media-type) -- [HTTP PUT vs HTTP PATCH in a REST API](http://www.baeldung.com/http-put-patch-difference-spring) -- [Exploring the Spring Boot TestRestTemplate](http://www.baeldung.com/spring-boot-testresttemplate) -- [Spring – Log Incoming Requests](http://www.baeldung.com/spring-http-logging) -- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations) -- [Introduction to CheckStyle](http://www.baeldung.com/checkstyle-java) -- [How to Change the Default Port in Spring Boot](http://www.baeldung.com/spring-boot-change-port) -- [Guide to DeferredResult in Spring](http://www.baeldung.com/spring-deferred-result) -- [Spring Custom Property Editor](http://www.baeldung.com/spring-mvc-custom-property-editor) -- [Using the Spring RestTemplate Interceptor](http://www.baeldung.com/spring-rest-template-interceptor) -- [Configure a RestTemplate with RestTemplateBuilder](http://www.baeldung.com/spring-rest-template-builder) - +- [Uploading MultipartFile with Spring RestTemplate](http://www.baeldung.com/uploading-multipartfile-with-spring-resttemplate/) From 8041d439258f29cb5c54474c4518cb0b3cc6f654 Mon Sep 17 00:00:00 2001 From: micropatel Date: Wed, 18 Jul 2018 00:54:36 -0300 Subject: [PATCH 3/5] Updated README.md file --- spring-rest-template/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-rest-template/README.md b/spring-rest-template/README.md index 698949c142..9c3ae4e7e2 100644 --- a/spring-rest-template/README.md +++ b/spring-rest-template/README.md @@ -1,4 +1,4 @@ -## Spring REST Templat Example Project +## Spring REST Template Example Project ### The Course The "REST With Spring" Classes: http://bit.ly/restwithspring From 3c8086ed65cc944af39fff19be267ac4d31b09bc Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 18 Jul 2018 20:52:18 +0300 Subject: [PATCH 4/5] Update MultipartFileUploadClient.java --- .../baeldung/web/upload/client/MultipartFileUploadClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java index 547aec17a0..804013d4dc 100644 --- a/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java +++ b/spring-rest-template/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -58,4 +58,5 @@ public class MultipartFileUploadClient { Files.write(testFile, "Hello World !!, This is a test file.".getBytes()); return new FileSystemResource(testFile.toFile()); } + } From 315165154af56e61c14e6acc5e9a3e4b9083c8f5 Mon Sep 17 00:00:00 2001 From: Eric Martin Date: Wed, 18 Jul 2018 19:37:34 -0500 Subject: [PATCH 5/5] Update README.md --- spring-rest-template/README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/spring-rest-template/README.md b/spring-rest-template/README.md index 9c3ae4e7e2..bf35f0d32c 100644 --- a/spring-rest-template/README.md +++ b/spring-rest-template/README.md @@ -2,6 +2,3 @@ ### The Course The "REST With Spring" Classes: http://bit.ly/restwithspring - -### Relevant Articles: -- [Uploading MultipartFile with Spring RestTemplate](http://www.baeldung.com/uploading-multipartfile-with-spring-resttemplate/)