JAVA-4010: Added new module and moved 3 articles to it
This commit is contained in:
parent
a996154694
commit
e54423ce83
12
spring-web-modules/spring-resttemplate-3/.gitignore
vendored
Normal file
12
spring-web-modules/spring-resttemplate-3/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
11
spring-web-modules/spring-resttemplate-3/README.md
Normal file
11
spring-web-modules/spring-resttemplate-3/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
## Spring RestTemplate
|
||||||
|
|
||||||
|
This module contains articles about Spring RestTemplate
|
||||||
|
|
||||||
|
### The Course
|
||||||
|
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Uploading MultipartFile with Spring RestTemplate](https://www.baeldung.com/spring-rest-template-multipart-upload)
|
||||||
|
- [Get and Post Lists of Objects with RestTemplate](https://www.baeldung.com/spring-rest-template-list)
|
||||||
|
- [Download a Large File Through a Spring RestTemplate](https://www.baeldung.com/spring-resttemplate-download-large-file)
|
29
spring-web-modules/spring-resttemplate-3/pom.xml
Normal file
29
spring-web-modules/spring-resttemplate-3/pom.xml
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spring-resttemplate-3</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-resttemplate-3</name>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Spring Boot Dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.resttemplate.lists;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sample application used to demonstrate working with Lists and RestTemplate.
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class EmployeeApplication
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
SpringApplication.run(EmployeeApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
package com.baeldung.resttemplate.lists.client;
|
||||||
|
|
||||||
|
import com.baeldung.resttemplate.lists.dto.Employee;
|
||||||
|
import com.baeldung.resttemplate.lists.dto.EmployeeList;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application that shows how to use Lists with RestTemplate.
|
||||||
|
*/
|
||||||
|
public class EmployeeClient {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
EmployeeClient employeeClient = new EmployeeClient();
|
||||||
|
|
||||||
|
System.out.println("Calling GET for entity using arrays");
|
||||||
|
employeeClient.getForEntityEmployeesAsArray();
|
||||||
|
|
||||||
|
System.out.println("Calling GET using ParameterizedTypeReference");
|
||||||
|
employeeClient.getAllEmployeesUsingParameterizedTypeReference();
|
||||||
|
|
||||||
|
System.out.println("Calling GET using wrapper class");
|
||||||
|
employeeClient.getAllEmployeesUsingWrapperClass();
|
||||||
|
|
||||||
|
System.out.println("Calling POST using normal lists");
|
||||||
|
employeeClient.createEmployeesUsingLists();
|
||||||
|
|
||||||
|
System.out.println("Calling POST using wrapper class");
|
||||||
|
employeeClient.createEmployeesUsingWrapperClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeClient() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee[] getForEntityEmployeesAsArray() {
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
ResponseEntity<Employee[]> response =
|
||||||
|
restTemplate.getForEntity(
|
||||||
|
"http://localhost:8082/spring-rest/employees/",
|
||||||
|
Employee[].class);
|
||||||
|
|
||||||
|
Employee[] employees = response.getBody();
|
||||||
|
|
||||||
|
assert employees != null;
|
||||||
|
asList(employees).forEach(System.out::println);
|
||||||
|
|
||||||
|
return employees;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesUsingParameterizedTypeReference() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
ResponseEntity<List<Employee>> response =
|
||||||
|
restTemplate.exchange(
|
||||||
|
"http://localhost:8082/spring-rest/employees/",
|
||||||
|
HttpMethod.GET,
|
||||||
|
null,
|
||||||
|
new ParameterizedTypeReference<List<Employee>>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
List<Employee> employees = response.getBody();
|
||||||
|
|
||||||
|
assert employees != null;
|
||||||
|
employees.forEach(System.out::println);
|
||||||
|
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesUsingWrapperClass() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
EmployeeList response =
|
||||||
|
restTemplate.getForObject(
|
||||||
|
"http://localhost:8082/spring-rest/employees/v2",
|
||||||
|
EmployeeList.class);
|
||||||
|
|
||||||
|
List<Employee> employees = response.getEmployees();
|
||||||
|
|
||||||
|
employees.forEach(System.out::println);
|
||||||
|
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createEmployeesUsingLists() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
List<Employee> newEmployees = new ArrayList<>();
|
||||||
|
newEmployees.add(new Employee(3, "Intern"));
|
||||||
|
newEmployees.add(new Employee(4, "CEO"));
|
||||||
|
|
||||||
|
restTemplate.postForObject(
|
||||||
|
"http://localhost:8082/spring-rest/employees/",
|
||||||
|
newEmployees,
|
||||||
|
ResponseEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createEmployeesUsingWrapperClass() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
List<Employee> newEmployees = new ArrayList<>();
|
||||||
|
newEmployees.add(new Employee(3, "Intern"));
|
||||||
|
newEmployees.add(new Employee(4, "CEO"));
|
||||||
|
|
||||||
|
restTemplate.postForObject(
|
||||||
|
"http://localhost:8082/spring-rest/employees/v2/",
|
||||||
|
new EmployeeList(newEmployees),
|
||||||
|
ResponseEntity.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.resttemplate.lists.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.baeldung.resttemplate.lists.dto.Employee;
|
||||||
|
import com.baeldung.resttemplate.lists.dto.EmployeeList;
|
||||||
|
import com.baeldung.resttemplate.lists.service.EmployeeService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/employees")
|
||||||
|
public class EmployeeResource
|
||||||
|
{
|
||||||
|
@Autowired
|
||||||
|
private EmployeeService employeeService;
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, path = "/")
|
||||||
|
public List<Employee> getEmployees()
|
||||||
|
{
|
||||||
|
return employeeService.getAllEmployees();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, path = "/v2")
|
||||||
|
public EmployeeList getEmployeesUsingWrapperClass()
|
||||||
|
{
|
||||||
|
List<Employee> employees = employeeService.getAllEmployees();
|
||||||
|
return new EmployeeList(employees);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, path = "/")
|
||||||
|
public void addEmployees(@RequestBody List<Employee> employees)
|
||||||
|
{
|
||||||
|
employeeService.addEmployees(employees);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, path = "/v2")
|
||||||
|
public void addEmployeesUsingWrapperClass(@RequestBody EmployeeList employeeWrapper)
|
||||||
|
{
|
||||||
|
employeeService.addEmployees(employeeWrapper.getEmployees());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.resttemplate.lists.dto;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
public long id;
|
||||||
|
public String title;
|
||||||
|
|
||||||
|
public Employee()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(long id, String title)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return "Employee #" + id + "[" + title + "]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.resttemplate.lists.dto;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EmployeeList
|
||||||
|
{
|
||||||
|
public List<Employee> employees;
|
||||||
|
|
||||||
|
public EmployeeList()
|
||||||
|
{
|
||||||
|
employees = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeList(List<Employee> employees)
|
||||||
|
{
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployees(List<Employee> employees)
|
||||||
|
{
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getEmployees()
|
||||||
|
{
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.resttemplate.lists.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.baeldung.resttemplate.lists.dto.Employee;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service("EmployeeListService")
|
||||||
|
public class EmployeeService
|
||||||
|
{
|
||||||
|
public List<Employee> getAllEmployees()
|
||||||
|
{
|
||||||
|
List<Employee> employees = new ArrayList<>();
|
||||||
|
employees.add(new Employee(1, "Manager"));
|
||||||
|
employees.add(new Employee(2, "Java Developer"));
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addEmployees(List<Employee> employees)
|
||||||
|
{
|
||||||
|
employees.forEach(e -> System.out.println("Adding new employee " + e));
|
||||||
|
}
|
||||||
|
}
|
@ -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 UploadApplication extends SpringBootServletInitializer {
|
||||||
|
|
||||||
|
public static void main(final String[] args) {
|
||||||
|
SpringApplication.run(UploadApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -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<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("file", getTestFile());
|
||||||
|
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||||
|
String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/";
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<String> 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<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("files", getTestFile());
|
||||||
|
body.add("files", getTestFile());
|
||||||
|
body.add("files", getTestFile());
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||||
|
String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/";
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<String> 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());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
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;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/fileserver")
|
||||||
|
public class FileServerResource {
|
||||||
|
|
||||||
|
@RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST)
|
||||||
|
public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile file) throws IOException {
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST)
|
||||||
|
public ResponseEntity<String> processFile(@RequestParam("files") List<MultipartFile> 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));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
server.port=8082
|
||||||
|
server.servlet.context-path=/spring-rest
|
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
|
||||||
|
<level value="DEBUG" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<logger name="org.springframework" level="WARN" />
|
||||||
|
<logger name="org.springframework.transaction" level="WARN" />
|
||||||
|
|
||||||
|
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||||
|
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = { com.baeldung.web.upload.app.UploadApplication.class, })
|
||||||
|
public class SpringContextTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
package com.baeldung.largefile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.util.StreamUtils;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
public class LargeFileDownloadIntegrationTest {
|
||||||
|
|
||||||
|
static String FILE_URL = "http://ovh.net/files/1Mio.dat";
|
||||||
|
|
||||||
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
restTemplate = new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResumableUrl_whenUrlCalledByHeadOption_thenExpectHeadersAvailable() {
|
||||||
|
HttpHeaders headers = restTemplate.headForHeaders(FILE_URL);
|
||||||
|
Assertions
|
||||||
|
.assertThat(headers.get("Accept-Ranges"))
|
||||||
|
.contains("bytes");
|
||||||
|
Assertions
|
||||||
|
.assertThat(headers.getContentLength())
|
||||||
|
.isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResumableUrl_whenDownloadCompletely_thenExpectCorrectFileSize() {
|
||||||
|
HttpHeaders headers = restTemplate.headForHeaders(FILE_URL);
|
||||||
|
long contentLength = headers.getContentLength();
|
||||||
|
File file = restTemplate.execute(FILE_URL, HttpMethod.GET, null, clientHttpResponse -> {
|
||||||
|
File ret = File.createTempFile("download", "tmp");
|
||||||
|
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.assertNotNull(file);
|
||||||
|
Assertions
|
||||||
|
.assertThat(file.length())
|
||||||
|
.isEqualTo(contentLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResumableUrl_whenDownloadRange_thenExpectFileSizeEqualOrLessThanRange() {
|
||||||
|
int range = 10;
|
||||||
|
File file = restTemplate.execute(FILE_URL, HttpMethod.GET, clientHttpRequest -> clientHttpRequest
|
||||||
|
.getHeaders()
|
||||||
|
.set("Range", String.format("bytes=0-%d", range - 1)), clientHttpResponse -> {
|
||||||
|
File ret = File.createTempFile("download", "tmp");
|
||||||
|
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.assertNotNull(file);
|
||||||
|
Assertions
|
||||||
|
.assertThat(file.length())
|
||||||
|
.isLessThanOrEqualTo(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResumableUrl_whenPauseDownloadAndResume_thenExpectCorrectFileSize() {
|
||||||
|
|
||||||
|
int range = 10;
|
||||||
|
|
||||||
|
HttpHeaders headers = restTemplate.headForHeaders(FILE_URL);
|
||||||
|
long contentLength = headers.getContentLength();
|
||||||
|
|
||||||
|
File file = restTemplate.execute(FILE_URL, HttpMethod.GET, clientHttpRequest -> clientHttpRequest
|
||||||
|
.getHeaders()
|
||||||
|
.set("Range", String.format("bytes=0-%d", range - 1)), clientHttpResponse -> {
|
||||||
|
File ret = File.createTempFile("download", "tmp");
|
||||||
|
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.assertNotNull(file);
|
||||||
|
|
||||||
|
Assertions
|
||||||
|
.assertThat(file.length())
|
||||||
|
.isLessThanOrEqualTo(range);
|
||||||
|
|
||||||
|
restTemplate.execute(FILE_URL, HttpMethod.GET, clientHttpRequest -> clientHttpRequest
|
||||||
|
.getHeaders()
|
||||||
|
.set("Range", String.format("bytes=%d-%d", file.length(), contentLength)), clientHttpResponse -> {
|
||||||
|
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(file, true));
|
||||||
|
return file;
|
||||||
|
});
|
||||||
|
|
||||||
|
Assertions
|
||||||
|
.assertThat(file.length())
|
||||||
|
.isEqualTo(contentLength);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
spring-web-modules/spring-resttemplate-3/src/test/resources/.gitignore
vendored
Normal file
13
spring-web-modules/spring-resttemplate-3/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
|
||||||
|
<level value="DEBUG" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<logger name="org.springframework" level="WARN" />
|
||||||
|
<logger name="org.springframework.transaction" level="WARN" />
|
||||||
|
|
||||||
|
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||||
|
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user