Skip to content

Commit 67cf33a

Browse files
committed
Added spring-rest-template
1 parent 5d7b57c commit 67cf33a

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

‎spring-rest-template/.gitignore‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
*/.idea/*
10+
11+
# Packaged files #
12+
*.jar
13+
*.war
14+
*.ear

‎spring-rest-template/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Spring REST Templat Example Project
2+
3+
### The Course
4+
The "REST With Spring" Classes: http://bit.ly/restwithspring
5+
6+
### Relevant Articles:
7+
- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping)
8+
- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
9+
- [Redirect in Spring](http://www.baeldung.com/spring-redirect-and-forward)
10+
- [Returning Custom Status Codes from Spring Controllers](http://www.baeldung.com/spring-mvc-controller-custom-http-status-code)
11+
- [A Guide to OkHttp](http://www.baeldung.com/guide-to-okhttp)
12+
- [Binary Data Formats in a Spring REST API](http://www.baeldung.com/spring-rest-api-with-binary-data-formats)
13+
- [Guide to UriComponentsBuilder in Spring](http://www.baeldung.com/spring-uricomponentsbuilder)
14+
- [Introduction to FindBugs](http://www.baeldung.com/intro-to-findbugs)
15+
- [A Custom Media Type for a Spring REST API](http://www.baeldung.com/spring-rest-custom-media-type)
16+
- [HTTP PUT vs HTTP PATCH in a REST API](http://www.baeldung.com/http-put-patch-difference-spring)
17+
- [Exploring the Spring Boot TestRestTemplate](http://www.baeldung.com/spring-boot-testresttemplate)
18+
- [Spring – Log Incoming Requests](http://www.baeldung.com/spring-http-logging)
19+
- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations)
20+
- [Introduction to CheckStyle](http://www.baeldung.com/checkstyle-java)
21+
- [How to Change the Default Port in Spring Boot](http://www.baeldung.com/spring-boot-change-port)
22+
- [Guide to DeferredResult in Spring](http://www.baeldung.com/spring-deferred-result)
23+
- [Spring Custom Property Editor](http://www.baeldung.com/spring-mvc-custom-property-editor)
24+
- [Using the Spring RestTemplate Interceptor](http://www.baeldung.com/spring-rest-template-interceptor)
25+
- [Configure a RestTemplate with RestTemplateBuilder](http://www.baeldung.com/spring-rest-template-builder)
26+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
4+
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
5+
<module name="Checker">
6+
<module name="TreeWalker">
7+
<module name="AvoidStarImport">
8+
<property name="severity" value="warning" />
9+
</module>
10+
</module>
11+
</module>

‎spring-rest-template/pom.xml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-rest-template</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<name>spring-rest-template</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<artifactId>parent-boot-2</artifactId>
12+
<groupId>com.baeldung</groupId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-boot-2</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
19+
<!-- Spring -->
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-web</artifactId>
23+
<exclusions>
24+
<exclusion>
25+
<artifactId>commons-logging</artifactId>
26+
<groupId>commons-logging</groupId>
27+
</exclusion>
28+
</exclusions>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<finalName>spring-rest-template</finalName>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-checkstyle-plugin</artifactId>
38+
<version>${checkstyle-maven-plugin.version}</version>
39+
<configuration>
40+
<configLocation>checkstyle.xml</configLocation>
41+
</configuration>
42+
<executions>
43+
<execution>
44+
<goals>
45+
<goal>check</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-surefire-plugin</artifactId>
53+
<configuration>
54+
<forkCount>3</forkCount>
55+
<reuseForks>true</reuseForks>
56+
<excludes>
57+
<exclude>**/*IntegrationTest.java</exclude>
58+
<exclude>**/*LongRunningUnitTest.java</exclude>
59+
<exclude>**/*ManualTest.java</exclude>
60+
<exclude>**/*LiveTest.java</exclude>
61+
</excludes>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
<reporting>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-checkstyle-plugin</artifactId>
71+
<version>${checkstyle-maven-plugin.version}</version>
72+
<configuration>
73+
<configLocation>checkstyle.xml</configLocation>
74+
</configuration>
75+
</plugin>
76+
</plugins>
77+
</reporting>
78+
79+
<properties>
80+
<!-- Maven plugins -->
81+
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
82+
</properties>
83+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.web.upload.client;
2+
3+
import org.springframework.core.io.FileSystemResource;
4+
import org.springframework.core.io.Resource;
5+
import org.springframework.http.HttpEntity;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.util.LinkedMultiValueMap;
10+
import org.springframework.util.MultiValueMap;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
17+
public class MultipartFileUploadClient {
18+
19+
public static void main(String[] args) throws IOException {
20+
uploadSingleFile();
21+
uploadMultipleFile();
22+
}
23+
24+
private static void uploadSingleFile() throws IOException {
25+
HttpHeaders headers = new HttpHeaders();
26+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
27+
28+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
29+
body.add("file", getTestFile());
30+
31+
32+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
33+
String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/";
34+
RestTemplate restTemplate = new RestTemplate();
35+
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
36+
System.out.println("Response code: " + response.getStatusCode());
37+
}
38+
39+
private static void uploadMultipleFile() throws IOException {
40+
HttpHeaders headers = new HttpHeaders();
41+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
42+
43+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
44+
body.add("files", getTestFile());
45+
body.add("files", getTestFile());
46+
body.add("files", getTestFile());
47+
48+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
49+
String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/";
50+
RestTemplate restTemplate = new RestTemplate();
51+
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
52+
System.out.println("Response code: " + response.getStatusCode());
53+
}
54+
55+
public static Resource getTestFile() throws IOException {
56+
Path testFile = Files.createTempFile("test-file", ".txt");
57+
System.out.println("Creating and Uploading Test File: " + testFile);
58+
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
59+
return new FileSystemResource(testFile.toFile());
60+
}
61+
}

0 commit comments

Comments
 (0)