Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 1577990

Browse files
author
Artem Labazin
committed
refactoring
1 parent 3c3951d commit 1577990

File tree

4 files changed

+77
-63
lines changed

4 files changed

+77
-63
lines changed

feign-form-spring/src/test/java/feign/form/feign/spring/Server.java

+30-35
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@
2121
import static org.springframework.http.HttpStatus.OK;
2222
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
2323
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
24-
import static org.springframework.web.bind.annotation.RequestMethod.GET;
25-
import static org.springframework.web.bind.annotation.RequestMethod.POST;
2624

2725
import java.io.IOException;
2826
import java.util.Map;
2927

30-
import lombok.SneakyThrows;
3128
import lombok.val;
3229
import org.springframework.boot.autoconfigure.SpringBootApplication;
3330
import org.springframework.cloud.openfeign.EnableFeignClients;
@@ -39,9 +36,10 @@
3936
import org.springframework.http.ResponseEntity;
4037
import org.springframework.util.LinkedMultiValueMap;
4138
import org.springframework.util.MultiValueMap;
39+
import org.springframework.web.bind.annotation.GetMapping;
4240
import org.springframework.web.bind.annotation.PathVariable;
41+
import org.springframework.web.bind.annotation.PostMapping;
4342
import org.springframework.web.bind.annotation.RequestBody;
44-
import org.springframework.web.bind.annotation.RequestMapping;
4543
import org.springframework.web.bind.annotation.RequestParam;
4644
import org.springframework.web.bind.annotation.RequestPart;
4745
import org.springframework.web.bind.annotation.RestController;
@@ -53,55 +51,53 @@
5351
@SuppressWarnings("checkstyle:DesignForExtension")
5452
public class Server {
5553

56-
@RequestMapping(
54+
@PostMapping(
5755
path = "/multipart/upload1/{folder}",
58-
method = POST,
5956
consumes = MULTIPART_FORM_DATA_VALUE
6057
)
61-
@SneakyThrows
62-
public String upload1 (@PathVariable("folder") String folder,
63-
@RequestPart("file") MultipartFile file,
64-
@RequestParam(value = "message", required = false) String message
65-
) {
58+
public String upload1 (
59+
@PathVariable("folder") String folder,
60+
@RequestPart("file") MultipartFile file,
61+
@RequestParam(value = "message", required = false) String message
62+
) throws IOException {
6663
return new String(file.getBytes()) + ':' + message + ':' + folder;
6764
}
6865

69-
@RequestMapping(
66+
@PostMapping(
7067
path = "/multipart/upload2/{folder}",
71-
method = POST,
7268
consumes = MULTIPART_FORM_DATA_VALUE
7369
)
74-
@SneakyThrows
75-
public String upload2 (@RequestBody MultipartFile file,
76-
@PathVariable("folder") String folder,
77-
@RequestParam(value = "message", required = false) String message
78-
) {
70+
public String upload2 (
71+
@RequestBody MultipartFile file,
72+
@PathVariable("folder") String folder,
73+
@RequestParam(value = "message", required = false) String message
74+
) throws IOException {
7975
return new String(file.getBytes()) + ':' + message + ':' + folder;
8076
}
8177

82-
@RequestMapping(
78+
@PostMapping(
8379
path = "/multipart/upload3/{folder}",
84-
method = POST,
8580
consumes = MULTIPART_FORM_DATA_VALUE
8681
)
87-
public String upload3 (@RequestBody MultipartFile file,
88-
@PathVariable("folder") String folder,
89-
@RequestParam(value = "message", required = false) String message
82+
public String upload3 (
83+
@RequestBody MultipartFile file,
84+
@PathVariable("folder") String folder,
85+
@RequestParam(value = "message", required = false) String message
9086
) {
9187
return file.getOriginalFilename() + ':' + file.getContentType() + ':' + folder;
9288
}
9389

94-
@RequestMapping(path = "/multipart/upload4/{id}", method = POST)
95-
public String upload4 (@PathVariable("id") String id,
96-
@RequestBody Map<String, Object> map,
97-
@RequestParam String userName
90+
@PostMapping("/multipart/upload4/{id}")
91+
public String upload4 (
92+
@PathVariable("id") String id,
93+
@RequestBody Map<String, Object> map,
94+
@RequestParam String userName
9895
) {
9996
return userName + ':' + id + ':' + map.size();
10097
}
10198

102-
@RequestMapping(
99+
@PostMapping(
103100
path = "/multipart/upload5",
104-
method = POST,
105101
consumes = MULTIPART_FORM_DATA_VALUE
106102
)
107103
void upload5 (Dto dto) throws IOException {
@@ -111,13 +107,13 @@ void upload5 (Dto dto) throws IOException {
111107
assert "Hello world".equals(new String(dto.getFile().getBytes(), UTF_8));
112108
}
113109

114-
@RequestMapping(
110+
@PostMapping(
115111
path = "/multipart/upload6",
116-
method = POST,
117112
consumes = MULTIPART_FORM_DATA_VALUE
118113
)
119-
public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa1,
120-
@RequestParam("popa2") MultipartFile popa2
114+
public ResponseEntity<String> upload6 (
115+
@RequestParam("popa1") MultipartFile popa1,
116+
@RequestParam("popa2") MultipartFile popa2
121117
) throws Exception {
122118
HttpStatus status = I_AM_A_TEAPOT;
123119
String result = "";
@@ -128,9 +124,8 @@ public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa
128124
return ResponseEntity.status(status).body(result);
129125
}
130126

131-
@RequestMapping(
127+
@GetMapping(
132128
path = "/multipart/download/{fileId}",
133-
method = GET,
134129
produces = MULTIPART_FORM_DATA_VALUE
135130
)
136131
public MultiValueMap<String, Object> download (@PathVariable("fileId") String fileId) {

feign-form/src/main/java/feign/form/multipart/AbstractWriter.java

-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.net.URLConnection;
2222

23-
import lombok.SneakyThrows;
2423
import lombok.val;
2524

2625
import feign.codec.EncodeException;
@@ -63,7 +62,6 @@ protected void write (Output output, String key, Object value) throws EncodeExce
6362
* @param fileName file name.
6463
* @param contentType type of file content. May be the {@code null}, in that case it will be determined by file name.
6564
*/
66-
@SneakyThrows
6765
protected void writeFileMetadata (Output output, String name, String fileName, String contentType) {
6866
val contentDespositionBuilder = new StringBuilder()
6967
.append("Content-Disposition: form-data; name=\"").append(name).append("\"");

feign-form/src/main/java/feign/form/multipart/Output.java

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public Output write (byte[] bytes) {
7575
*
7676
* @return this output
7777
*/
78-
@SneakyThrows
7978
public Output write (byte[] bytes, int offset, int length) {
8079
outputStream.write(bytes, offset, length);
8180
return this;

feign-form/src/test/java/feign/form/Server.java

+47-25
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
2626
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
2727
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
28-
import static org.springframework.web.bind.annotation.RequestMethod.POST;
2928

3029
import java.io.IOException;
3130
import java.util.Collection;
@@ -36,10 +35,10 @@
3635
import org.springframework.http.HttpStatus;
3736
import org.springframework.http.ResponseEntity;
3837
import org.springframework.stereotype.Controller;
38+
import org.springframework.web.bind.annotation.GetMapping;
3939
import org.springframework.web.bind.annotation.PathVariable;
4040
import org.springframework.web.bind.annotation.PostMapping;
4141
import org.springframework.web.bind.annotation.RequestBody;
42-
import org.springframework.web.bind.annotation.RequestMapping;
4342
import org.springframework.web.bind.annotation.RequestParam;
4443
import org.springframework.web.bind.annotation.RequestPart;
4544
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -51,21 +50,23 @@
5150
@SuppressWarnings("checkstyle:DesignForExtension")
5251
public class Server {
5352

54-
@RequestMapping(path = "/form", method = POST)
55-
public ResponseEntity<Void> form (@RequestParam("key1") String key1,
56-
@RequestParam("key2") String key2
53+
@PostMapping("/form")
54+
public ResponseEntity<Void> form (
55+
@RequestParam("key1") String key1,
56+
@RequestParam("key2") String key2
5757
) {
5858
val status = !key1.equals(key2)
5959
? BAD_REQUEST
6060
: OK;
6161
return ResponseEntity.status(status).body(null);
6262
}
6363

64-
@RequestMapping(path = "/upload/{id}", method = POST)
64+
@PostMapping("/upload/{id}")
6565
@ResponseStatus(OK)
66-
public ResponseEntity<Long> upload (@PathVariable("id") Integer id,
67-
@RequestParam("public") Boolean isPublic,
68-
@RequestParam("file") MultipartFile file
66+
public ResponseEntity<Long> upload (
67+
@PathVariable("id") Integer id,
68+
@RequestParam("public") Boolean isPublic,
69+
@RequestParam("file") MultipartFile file
6970
) {
7071
HttpStatus status;
7172
if (id == null || id != 10) {
@@ -82,7 +83,7 @@ public ResponseEntity<Long> upload (@PathVariable("id") Integer id,
8283
return ResponseEntity.status(status).body(file.getSize());
8384
}
8485

85-
@RequestMapping(path = "/upload", method = POST)
86+
@PostMapping("/upload")
8687
public ResponseEntity<Long> upload (@RequestParam("file") MultipartFile file) {
8788
HttpStatus status;
8889
if (file.getSize() == 0) {
@@ -95,7 +96,7 @@ public ResponseEntity<Long> upload (@RequestParam("file") MultipartFile file) {
9596
return ResponseEntity.status(status).body(file.getSize());
9697
}
9798

98-
@RequestMapping(path = "/upload/files", method = POST)
99+
@PostMapping("/upload/files")
99100
public ResponseEntity<Long> upload (@RequestParam("files") MultipartFile[] files) {
100101
HttpStatus status;
101102
if (files[0].getSize() == 0 || files[1].getSize() == 0) {
@@ -109,7 +110,7 @@ public ResponseEntity<Long> upload (@RequestParam("files") MultipartFile[] files
109110
return ResponseEntity.status(status).body(files[0].getSize() + files[1].getSize());
110111
}
111112

112-
@RequestMapping(path = "/json", method = POST, consumes = APPLICATION_JSON_VALUE)
113+
@PostMapping(path = "/json", consumes = APPLICATION_JSON_VALUE)
113114
public ResponseEntity<String> json (@RequestBody Dto dto) {
114115
HttpStatus status;
115116
if (!dto.getName().equals("Artem")) {
@@ -122,24 +123,31 @@ public ResponseEntity<String> json (@RequestBody Dto dto) {
122123
return ResponseEntity.status(status).body("ok");
123124
}
124125

125-
@RequestMapping("/query_map")
126-
public ResponseEntity<Integer> queryMap (@RequestParam("filter") List<String> filters) {
126+
@GetMapping("/query_map")
127+
public ResponseEntity<Integer> queryMap (
128+
@RequestParam("filter") List<String> filters
129+
) {
127130
val status = filters != null && !filters.isEmpty()
128131
? OK
129132
: I_AM_A_TEAPOT;
130133
return ResponseEntity.status(status).body(filters.size());
131134
}
132135

133-
@RequestMapping(path = "/wild-card-map", method = POST, consumes = APPLICATION_FORM_URLENCODED_VALUE)
134-
public ResponseEntity<Integer> wildCardMap (@RequestParam("key1") String key1, @RequestParam("key2") String key2) {
136+
@PostMapping(path = "/wild-card-map", consumes = APPLICATION_FORM_URLENCODED_VALUE)
137+
public ResponseEntity<Integer> wildCardMap (
138+
@RequestParam("key1") String key1,
139+
@RequestParam("key2") String key2
140+
) {
135141
val status = key1.equals(key2)
136142
? OK
137143
: I_AM_A_TEAPOT;
138144
return ResponseEntity.status(status).body(null);
139145
}
140146

141147
@PostMapping(path = "/upload/with_dto", consumes = MULTIPART_FORM_DATA_VALUE)
142-
public ResponseEntity<Long> uploadWithDto (Dto dto, @RequestPart("file") MultipartFile file
148+
public ResponseEntity<Long> uploadWithDto (
149+
Dto dto,
150+
@RequestPart("file") MultipartFile file
143151
) throws IOException {
144152
val status = dto != null && dto.getName().equals("Artem")
145153
? OK
@@ -148,7 +156,9 @@ public ResponseEntity<Long> uploadWithDto (Dto dto, @RequestPart("file") Multipa
148156
}
149157

150158
@PostMapping(path = "/upload/byte_array", consumes = MULTIPART_FORM_DATA_VALUE)
151-
public ResponseEntity<String> uploadByteArray (@RequestPart("file") MultipartFile file) {
159+
public ResponseEntity<String> uploadByteArray (
160+
@RequestPart("file") MultipartFile file
161+
) {
152162
val status = file != null
153163
? OK
154164
: I_AM_A_TEAPOT;
@@ -159,31 +169,39 @@ public ResponseEntity<String> uploadByteArray (@RequestPart("file") MultipartFil
159169
// We just want the request because when there's a filename part of the Content-Disposition header spring
160170
// will treat it as a file (available through getFile()) and when it doesn't have the filename part it's
161171
// available in the parameter (getParameter())
162-
public ResponseEntity<String> uploadByteArrayParameter (MultipartHttpServletRequest request) {
172+
public ResponseEntity<String> uploadByteArrayParameter (
173+
MultipartHttpServletRequest request
174+
) {
163175
val status = request.getFile("file") == null && request.getParameter("file") != null
164176
? OK
165177
: I_AM_A_TEAPOT;
166178
return ResponseEntity.status(status).build();
167179
}
168180

169181
@PostMapping(path = "/upload/unknown_type", consumes = MULTIPART_FORM_DATA_VALUE)
170-
public ResponseEntity<String> uploadUnknownType (@RequestPart("file") MultipartFile file) {
182+
public ResponseEntity<String> uploadUnknownType (
183+
@RequestPart("file") MultipartFile file
184+
) {
171185
val status = file != null
172186
? OK
173187
: I_AM_A_TEAPOT;
174188
return ResponseEntity.status(status).body(file.getContentType());
175189
}
176190

177191
@PostMapping(path = "/upload/form_data", consumes = MULTIPART_FORM_DATA_VALUE)
178-
public ResponseEntity<String> uploadFormData (@RequestPart("file") MultipartFile file) {
192+
public ResponseEntity<String> uploadFormData (
193+
@RequestPart("file") MultipartFile file
194+
) {
179195
val status = file != null
180196
? OK
181197
: I_AM_A_TEAPOT;
182198
return ResponseEntity.status(status).body(file.getOriginalFilename() + ':' + file.getContentType());
183199
}
184200

185201
@PostMapping(path = "/submit/url", consumes = APPLICATION_FORM_URLENCODED_VALUE)
186-
public ResponseEntity<String> submitRepeatableQueryParam (@RequestParam("names") String[] names) {
202+
public ResponseEntity<String> submitRepeatableQueryParam (
203+
@RequestParam("names") String[] names
204+
) {
187205
val response = new StringBuilder();
188206
if (names != null && names.length == 2) {
189207
response
@@ -199,7 +217,9 @@ public ResponseEntity<String> submitRepeatableQueryParam (@RequestParam("names")
199217
}
200218

201219
@PostMapping(path = "/submit/form", consumes = MULTIPART_FORM_DATA_VALUE)
202-
public ResponseEntity<String> submitRepeatableFormParam (@RequestParam("names") Collection<String> names) {
220+
public ResponseEntity<String> submitRepeatableFormParam (
221+
@RequestParam("names") Collection<String> names
222+
) {
203223
val response = new StringBuilder();
204224
if (names != null && names.size() == 2) {
205225
val iterator = names.iterator();
@@ -216,8 +236,10 @@ public ResponseEntity<String> submitRepeatableFormParam (@RequestParam("names")
216236
}
217237

218238
@PostMapping(path = "/form-data", consumes = APPLICATION_FORM_URLENCODED_VALUE)
219-
public ResponseEntity<String> submitPostData (@RequestParam("f_name") String firstName,
220-
@RequestParam("age") Integer age) {
239+
public ResponseEntity<String> submitPostData (
240+
@RequestParam("f_name") String firstName,
241+
@RequestParam("age") Integer age
242+
) {
221243
val response = new StringBuilder();
222244
if (firstName != null && age != null) {
223245
response

0 commit comments

Comments
 (0)