BAEL-842 improvement to the spring controller return image or file in controller example: add a dynamic example

This commit is contained in:
thibault.faure 2022-06-29 11:11:36 +02:00
parent b6e04505ef
commit 9d59e267ac
6 changed files with 74 additions and 0 deletions

View File

@ -40,6 +40,18 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>/src/main/resources</directory>
</resource>
</resources>
</build>
</project>

View File

@ -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<InputStreamResource> 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");

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -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));
}
}