From 9d59e267ac7841e08331eeb1bd6a99800f2f5cb0 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Wed, 29 Jun 2022 11:11:36 +0200 Subject: [PATCH] BAEL-842 improvement to the spring controller return image or file in controller example: add a dynamic example --- spring-boot-modules/spring-boot-mvc-3/pom.xml | 12 +++++ .../controller/DataProducerController.java | 15 ++++++ .../com/baeldung/produceimage/data.txt | 1 + .../com/baeldung/produceimage/image.jpg | Bin 0 -> 8310 bytes .../com/baeldung/produceimage/image.png | Bin 0 -> 4567 bytes ...DataProducerControllerIntegrationTest.java | 46 ++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-mvc-3/pom.xml b/spring-boot-modules/spring-boot-mvc-3/pom.xml index 43a492786e..f2b6c129f8 100644 --- a/spring-boot-modules/spring-boot-mvc-3/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-3/pom.xml @@ -40,6 +40,18 @@ commons-io ${commons-io.version} + + org.springframework.boot + spring-boot-starter-test + test + + + + + /src/main/resources + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java index ab233a8b60..218be68a45 100644 --- a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java @@ -1,9 +1,12 @@ package com.baeldung.produceimage.controller; import org.apache.commons.io.IOUtils; +import org.springframework.core.io.InputStreamResource; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; @@ -29,6 +32,18 @@ public class DataProducerController { return IOUtils.toByteArray(in); } + @GetMapping("/get-image-dynamic-type") + @ResponseBody + public ResponseEntity getImageDynamicType(@RequestParam("jpg") boolean jpg) { + final MediaType contentType = jpg ? MediaType.IMAGE_JPEG : MediaType.IMAGE_PNG; + final InputStream in = jpg ? + getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg") : + getClass().getResourceAsStream("/com/baeldung/produceimage/image.png"); + return ResponseEntity.ok() + .contentType(contentType) + .body(new InputStreamResource(in)); + } + @GetMapping(value = "/get-file", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public @ResponseBody byte[] getFile() throws IOException { final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/data.txt"); diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt new file mode 100644 index 0000000000..5a8297fc5e --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt @@ -0,0 +1 @@ +Hello Baeldung! \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg new file mode 100644 index 0000000000000000000000000000000000000000..db3abce0d6ac5d8088ae6ddc9d17037e7465198e GIT binary patch literal 8310 zcmbVxWmFu^)^)?+?hI}Nfgr&pxI+l;9vp%@1PKx#xC{*L5ZqltaF^ijmY{)2&;(xc z+~>RN{{Bu|ov!X)yUwXyYggCf(&H9@_exGt4nRNv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R+_ukTY-=OKhq|6{YAzOHJ#NY5d_x9t@OnD?-6 zw_S<6hzd_BTz|P%vH|)tmnI=wx)m~wh>E)mNa6;Mjmf@j00d=k2X@pXzpBRHxT1Ll zsa-m$Tg`DDR&BSw3*=~k83plO*}Rq!esR+C2IfaW9L>-nWVfjB7W8oKOzb?QTL8r~ zEdTzx<3dKt^`x$<--c(QXa1{5j;2Indy9Kp!p-2mC#m!DWJf)=Vd)379nI*CiL2{3 z~$H(Vs6SltN7? z4gBL?0h#_|5%8gwmX?;5mX?;5mX?;5mOIhu{2#Y65H^G1m%so3002ovPDHLkV1k2+ B)jR+I literal 0 HcmV?d00001 diff --git a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java new file mode 100644 index 0000000000..29f794645a --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.produceimage; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.springframework.web.context.WebApplicationContext; + +@SpringBootTest(classes = ImageApplication.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class DataProducerControllerIntegrationTest { + + @Autowired + private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @BeforeEach + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + void givenJpgTrue_whenGetImageDynamicType_ThenContentTypeIsJpg() throws Exception { + mockMvc.perform(get("/get-image-dynamic-type?jpg=true")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.IMAGE_JPEG)) + .andExpect(header().stringValues(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_JPEG_VALUE)); + } + + @Test + void givenJpgFalse_whenGetImageDynamicType_ThenContentTypeIsFalse() throws Exception { + mockMvc.perform(get("/get-image-dynamic-type?jpg=false")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.IMAGE_PNG)) + .andExpect(header().stringValues(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)); + } + +}