From c6acca67144c66d8cf3e836c2d0766d0010d7ea2 Mon Sep 17 00:00:00 2001 From: Justin Albano Date: Sat, 7 Dec 2019 09:08:35 -0500 Subject: [PATCH] BAEL-3559: Change server response to mimic an echo server --- .../MultipartFileUploadStubController.java | 61 +++++++++++++------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/MultipartFileUploadStubController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/MultipartFileUploadStubController.java index cac1ece5df..28c35e8603 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/MultipartFileUploadStubController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/MultipartFileUploadStubController.java @@ -1,33 +1,58 @@ package com.baeldung.web.controller; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.multipart.MultipartFile; @Controller public class MultipartFileUploadStubController { - - private static final Logger logger = LoggerFactory.getLogger(MultipartFileUploadStubController.class); @PostMapping("/stub/multipart") - @ResponseStatus(HttpStatus.OK) - public void uploadFile(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) { - logger.info("Uploaded file: " + format(file)); - logger.info(" - text: [" + format(text) + "]"); - logger.info(" - text1: [" + format(text1) + "]"); - logger.info(" - text2: [" + format(text2) + "]"); - logger.info(" - upstream: [" + format(upstream) + "]"); + public ResponseEntity uploadFile(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) { + UploadResultResource result = new UploadResultResource(file, text, text1, text2, upstream); + return new ResponseEntity<>(result, HttpStatus.OK); } - private static String format(MultipartFile file) { - return file == null ? "" : file.getOriginalFilename() + " (size: " + file.getSize() + " bytes)"; - } - - private static String format(String str) { - return str == null ? "" : str; + public static class UploadResultResource { + + private final String file; + private final String text; + private final String text1; + private final String text2; + private final String upstream; + + public UploadResultResource(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) { + this.file = format(file); + this.text = text; + this.text1 = text1; + this.text2 = text2; + this.upstream = format(upstream); + } + + private static String format(MultipartFile file) { + return file == null ? null : file.getOriginalFilename() + " (size: " + file.getSize() + " bytes)"; + } + + public String getFile() { + return file; + } + + public String getText() { + return text; + } + + public String getText1() { + return text1; + } + + public String getText2() { + return text2; + } + + public String getUpstream() { + return upstream; + } } }