From b8c78d9a8e6d3f2151410b1a5bf31dd2ec16f004 Mon Sep 17 00:00:00 2001 From: micropatel Date: Sun, 17 Jun 2018 18:00:47 -0300 Subject: [PATCH 1/7] Added Simple Reactive Event Server and Client Example --- .../reactive/ReactiveEventClient.java | 29 +++++++++++++++++++ .../controller/ReactiveEventServer.java | 21 ++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java new file mode 100644 index 0000000000..6e421d7f5f --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java @@ -0,0 +1,29 @@ +package com.baeldung.reactive; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; + +@SpringBootApplication +public class ReactiveEventClient implements CommandLineRunner { + public static void main(String[] args) { + System.setProperty("server.port","8090"); + SpringApplication.run(ReactiveEventClient.class, args); + } + + @Override + public void run(String... args) throws Exception { + WebClient webclient = WebClient.builder() + .defaultHeader(HttpHeaders.ACCEPT, MediaType.TEXT_EVENT_STREAM_VALUE) + .baseUrl("http://localhost:8080/events") + .build(); + + webclient.get().uri("/stream/").header(HttpHeaders.CONTENT_TYPE,MediaType.TEXT_HTML.toString()) + .retrieve().bodyToFlux(String.class) + .subscribe(System.out::println); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java new file mode 100644 index 0000000000..2efde58c80 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java @@ -0,0 +1,21 @@ +package com.baeldung.reactive.controller; + + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; + +import java.time.Duration; + + +@RestController +@RequestMapping("/events") +public class ReactiveEventServer { + + @GetMapping(path = "/stream/" , produces=MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux eventStream() { + return Flux.interval(Duration.ofSeconds(1)).map(tick -> {return "Event:" + tick;}); + } +} \ No newline at end of file From 03c5e4c03aadd2f689ab0b3fa6b27dd3b0b1ce0a Mon Sep 17 00:00:00 2001 From: micropatel Date: Wed, 4 Jul 2018 22:00:38 -0300 Subject: [PATCH 2/7] Added Example Application For FileUpload using Spring RestTemplated --- .../baeldung/web/upload/app/Application.java | 17 ++++++++ .../client/MultipartFileUploadClient.java | 40 +++++++++++++++++++ .../upload/controller/FileServerResource.java | 28 +++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java b/spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java new file mode 100644 index 0000000000..d6821196eb --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/upload/app/Application.java @@ -0,0 +1,17 @@ +package com.baeldung.web.upload.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; + +@EnableAutoConfiguration +@ComponentScan("com.baeldung.web.upload") +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java new file mode 100644 index 0000000000..2045e80334 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -0,0 +1,40 @@ +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 { + MultiValueMap body = new LinkedMultiValueMap<>(); + HttpHeaders headers = new HttpHeaders(); + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + body.add("file", getTestFile()); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + String serverUrl = "http://localhost:8080/fileserver/"; + 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()); + } +} \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java b/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java new file mode 100644 index 0000000000..5198c80cff --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java @@ -0,0 +1,28 @@ +package com.baeldung.web.upload.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; + +@RestController +@RequestMapping("/fileserver") +public class FileServerResource { + + @RequestMapping(method = RequestMethod.POST) + public ResponseEntity processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException { + + byte[] bytes = multipartFile.getBytes(); + + System.out.println("File Name: " + multipartFile.getOriginalFilename()); + System.out.println("File Content Type: " + multipartFile.getContentType()); + System.out.println("File Content:\n" + new String(bytes)); + + return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); + } +} \ No newline at end of file From 733f3f50c8d6daa95be823e53dc05061ee106ac0 Mon Sep 17 00:00:00 2001 From: micropatel <31759369+micropatel@users.noreply.github.com> Date: Mon, 9 Jul 2018 19:30:46 -0300 Subject: [PATCH 3/7] Delete ReactiveEventClient.java --- .../reactive/ReactiveEventClient.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java deleted file mode 100644 index 6e421d7f5f..0000000000 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.reactive; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Flux; - -@SpringBootApplication -public class ReactiveEventClient implements CommandLineRunner { - public static void main(String[] args) { - System.setProperty("server.port","8090"); - SpringApplication.run(ReactiveEventClient.class, args); - } - - @Override - public void run(String... args) throws Exception { - WebClient webclient = WebClient.builder() - .defaultHeader(HttpHeaders.ACCEPT, MediaType.TEXT_EVENT_STREAM_VALUE) - .baseUrl("http://localhost:8080/events") - .build(); - - webclient.get().uri("/stream/").header(HttpHeaders.CONTENT_TYPE,MediaType.TEXT_HTML.toString()) - .retrieve().bodyToFlux(String.class) - .subscribe(System.out::println); - } -} From d9d9b5731c7749009c95a70c149f4a3fc6b61b7b Mon Sep 17 00:00:00 2001 From: micropatel <31759369+micropatel@users.noreply.github.com> Date: Mon, 9 Jul 2018 19:32:27 -0300 Subject: [PATCH 4/7] Delete ReactiveEventServer.java --- .../controller/ReactiveEventServer.java | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java deleted file mode 100644 index 2efde58c80..0000000000 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.reactive.controller; - - -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Flux; - -import java.time.Duration; - - -@RestController -@RequestMapping("/events") -public class ReactiveEventServer { - - @GetMapping(path = "/stream/" , produces=MediaType.TEXT_EVENT_STREAM_VALUE) - public Flux eventStream() { - return Flux.interval(Duration.ofSeconds(1)).map(tick -> {return "Event:" + tick;}); - } -} \ No newline at end of file From c719cf25f2245f8bf78eb01f15302bf4d7e5fcc1 Mon Sep 17 00:00:00 2001 From: micropatel Date: Tue, 10 Jul 2018 01:19:03 -0300 Subject: [PATCH 5/7] Updated Client and Server for Multiple File Upload --- .../client/MultipartFileUploadClient.java | 33 +++++++++++++++---- .../upload/controller/FileServerResource.java | 25 +++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java index 2045e80334..0e9f95ad33 100644 --- a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java +++ b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -1,6 +1,5 @@ package com.baeldung.web.upload.client; - import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; @@ -18,14 +17,36 @@ import java.nio.file.Path; public class MultipartFileUploadClient { public static void main(String[] args) throws IOException { - MultiValueMap body = new LinkedMultiValueMap<>(); - HttpHeaders headers = new HttpHeaders(); - HttpEntity> requestEntity = new HttpEntity<>(body, headers); + uploadSingleFile(); + uploadMultipleFile(); + } - body.add("file", getTestFile()); + private static void uploadSingleFile() throws IOException { + HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); - String serverUrl = "http://localhost:8080/fileserver/"; + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", getTestFile()); + + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String serverUrl = "http://localhost:8080/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:8080/fileserver/multiplefileupload/"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); System.out.println("Response code: " + response.getStatusCode()); diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java b/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java index 5198c80cff..3863a8f20d 100644 --- a/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java +++ b/spring-rest/src/main/java/com/baeldung/web/upload/controller/FileServerResource.java @@ -9,20 +9,35 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; +import java.util.List; @RestController @RequestMapping("/fileserver") public class FileServerResource { - @RequestMapping(method = RequestMethod.POST) - public ResponseEntity processFile(@RequestParam("file") MultipartFile multipartFile) throws IOException { + @RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST) + public ResponseEntity processFile(@RequestParam("file") MultipartFile file) throws IOException { - byte[] bytes = multipartFile.getBytes(); + byte[] bytes = file.getBytes(); - System.out.println("File Name: " + multipartFile.getOriginalFilename()); - System.out.println("File Content Type: " + multipartFile.getContentType()); + System.out.println("File Name: " + file.getOriginalFilename()); + System.out.println("File Content Type: " + file.getContentType()); System.out.println("File Content:\n" + new String(bytes)); return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); } + + @RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST) + public ResponseEntity processFile(@RequestParam("files") List files) throws IOException { + + for (MultipartFile file : files) { + byte[] bytes = file.getBytes(); + + System.out.println("File Name: " + file.getOriginalFilename()); + System.out.println("File Content Type: " + file.getContentType()); + System.out.println("File Content:\n" + new String(bytes)); + } + + return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); + } } \ No newline at end of file From dc14710c35059f9d146bb99f34606e2e0c5c1e0f Mon Sep 17 00:00:00 2001 From: micropatel <31759369+micropatel@users.noreply.github.com> Date: Wed, 11 Jul 2018 18:27:40 -0300 Subject: [PATCH 6/7] Updated port number --- .../web/upload/client/MultipartFileUploadClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java index 0e9f95ad33..c8c87d6ba8 100644 --- a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java +++ b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -30,7 +30,7 @@ public class MultipartFileUploadClient { HttpEntity> requestEntity = new HttpEntity<>(body, headers); - String serverUrl = "http://localhost:8080/fileserver/singlefileupload/"; + String serverUrl = "http://localhost:8082/fileserver/singlefileupload/"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); System.out.println("Response code: " + response.getStatusCode()); @@ -46,7 +46,7 @@ public class MultipartFileUploadClient { body.add("files", getTestFile()); HttpEntity> requestEntity = new HttpEntity<>(body, headers); - String serverUrl = "http://localhost:8080/fileserver/multiplefileupload/"; + String serverUrl = "http://localhost:8082/fileserver/multiplefileupload/"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); System.out.println("Response code: " + response.getStatusCode()); @@ -58,4 +58,4 @@ public class MultipartFileUploadClient { Files.write(testFile, "Hello World !!, This is a test file.".getBytes()); return new FileSystemResource(testFile.toFile()); } -} \ No newline at end of file +} From 5d7b57c46891604d75ed1e50d402914208ed3d17 Mon Sep 17 00:00:00 2001 From: micropatel Date: Wed, 11 Jul 2018 23:11:56 -0300 Subject: [PATCH 7/7] Updated port and applicaiton context --- .../baeldung/web/upload/client/MultipartFileUploadClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java index c8c87d6ba8..547aec17a0 100644 --- a/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java +++ b/spring-rest/src/main/java/com/baeldung/web/upload/client/MultipartFileUploadClient.java @@ -30,7 +30,7 @@ public class MultipartFileUploadClient { HttpEntity> requestEntity = new HttpEntity<>(body, headers); - String serverUrl = "http://localhost:8082/fileserver/singlefileupload/"; + 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()); @@ -46,7 +46,7 @@ public class MultipartFileUploadClient { body.add("files", getTestFile()); HttpEntity> requestEntity = new HttpEntity<>(body, headers); - String serverUrl = "http://localhost:8082/fileserver/multiplefileupload/"; + 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());