This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch logger-example in repository https://gitbox.apache.org/repos/asf/servicecomb-samples.git
commit 4746ed4745aebd66e885acabef821f8ae3e661b7 Author: liubao68 <[email protected]> AuthorDate: Sat May 29 09:46:19 2021 +0800 test upload error --- basic/consumer/pom.xml | 2 +- .../servicecomb/samples/ConsumerApplication.java | 4 +- .../samples/UploadSpringMvcConsumer.java | 177 +++++++++++++++++++++ basic/pom.xml | 2 +- basic/provider/pom.xml | 2 +- .../servicecomb/samples/ProviderApplication.java | 4 +- .../servicecomb/samples/UploadSpringmvcSchema.java | 132 +++++++++++++++ 7 files changed, 318 insertions(+), 5 deletions(-) diff --git a/basic/consumer/pom.xml b/basic/consumer/pom.xml index 2407914..f5a3e13 100644 --- a/basic/consumer/pom.xml +++ b/basic/consumer/pom.xml @@ -31,7 +31,7 @@ <dependencies> <dependency> <groupId>org.apache.servicecomb</groupId> - <artifactId>java-chassis-spring-boot-starter-servlet</artifactId> + <artifactId>java-chassis-spring-boot-starter-standalone</artifactId> </dependency> </dependencies> diff --git a/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java b/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java index b66c992..0010f13 100644 --- a/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java +++ b/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java @@ -19,14 +19,16 @@ package org.apache.servicecomb.samples; import org.apache.servicecomb.springboot2.starter.EnableServiceComb; import org.springframework.boot.SpringApplication; +import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication @EnableServiceComb public class ConsumerApplication { public static void main(String[] args) throws Exception { try { - SpringApplication.run(ConsumerApplication.class, args); + new SpringApplicationBuilder().web(WebApplicationType.NONE).sources(ConsumerApplication.class).run(args); } catch (Exception e) { e.printStackTrace(); } diff --git a/basic/consumer/src/main/java/org/apache/servicecomb/samples/UploadSpringMvcConsumer.java b/basic/consumer/src/main/java/org/apache/servicecomb/samples/UploadSpringMvcConsumer.java new file mode 100644 index 0000000..d73ce7d --- /dev/null +++ b/basic/consumer/src/main/java/org/apache/servicecomb/samples/UploadSpringMvcConsumer.java @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples; + +import static java.util.Collections.singletonList; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.commons.io.FileUtils; +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.core.io.FileSystemResource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +@RestSchema(schemaId = "UploadSpringMvcConsumer") +@RequestMapping(path = "/") +public class UploadSpringMvcConsumer { + private static final Logger LOGGER = LoggerFactory.getLogger(UploadSpringMvcConsumer.class); + + private FileSystemResource fileSystemResource1; + + private FileSystemResource fileSystemResource2; + + + private static final String message = "cseMessage"; + + public UploadSpringMvcConsumer() throws IOException { + File file1 = File.createTempFile("jaxrstest1", ".txt"); + File file2 = File.createTempFile("测试啊", ".txt"); + File file3 = File.createTempFile("files", ".yaml"); + File file4 = File.createTempFile("files4", ".yaml"); + FileUtils.writeStringToFile(file1, "hello1", StandardCharsets.UTF_8, false); + FileUtils.writeStringToFile(file2, "中文 2", StandardCharsets.UTF_8, false); + FileUtils.writeStringToFile(file3, "cse3", StandardCharsets.UTF_8, false); + FileUtils.writeStringToFile(file4, "cse4", StandardCharsets.UTF_8, false); + fileSystemResource1 = new FileSystemResource(file1); + fileSystemResource2 = new FileSystemResource(file2); + } + + FileSystemResource createFileSystemResource(String prefix, String suffix, String content) { + try { + File file = File.createTempFile(prefix, suffix); + FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8, false); + return new FileSystemResource(file); + } catch (IOException e) { + return null; + } + } + + @GetMapping("/uploadMix") + public String uploadMix() throws Exception { + LOGGER.info("================================================"); + CountDownLatch latch = new CountDownLatch(2); + AtomicBoolean failed = new AtomicBoolean(false); + + for (int i = 0; i < 2; i++) { + final int index = i; + new Thread() { + public void run() { + try { + Map<String, Object> map = new HashMap<>(); + List<FileSystemResource> fileList = new ArrayList<>(); + fileList.add(createFileSystemResource("测试啊-" + index, ".txt", "中文 2")); + map.put("file", createFileSystemResource("jaxrstest1-" + index, ".txt", "hello1")); + map.put("fileList", fileList); + map.put("str", message); + map.put("strList", singletonList("2.中文测试")); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + RestTemplate restTemplate = RestTemplateBuilder.create(); + ResponseEntity<Map<String, String>> response = + restTemplate + .exchange("servicecomb://provider/v1/uploadSpringmvcSchema/uploadMultiformMix", HttpMethod.POST, + new HttpEntity<>(map, headers), new ParameterizedTypeReference<Map<String, String>>() { + }); + Map<String, String> responseBody = response.getBody(); + String result = responseBody.get("file") + + responseBody.get("fileList") + + responseBody.get("str") + + responseBody.get("strList"); + + if (!containsAll(result, "hello1", "中文 2", message, "[2.中文测试]")) { + LOGGER.error("failed result is {}, index is ={}", result, index); + failed.set(true); + } + } catch (Exception e) { + LOGGER.error("failed result is {}, index is ={}", e.getMessage(), index); + failed.set(true); + } + latch.countDown(); + } + }.start(); + } + + latch.await(); + return "failed=" + failed.get(); + } + + @GetMapping("/upload") + public String upload() throws Exception { + CountDownLatch latch = new CountDownLatch(10); + AtomicBoolean failed = new AtomicBoolean(false); + + for (int i = 0; i < 10; i++) { + final int index = i; + new Thread() { + public void run() { + try { + Map<String, Object> map = new HashMap<>(); + map.put("file1", createFileSystemResource("jaxrstest1-" + index, ".txt", "hello1")); + map.put("file2", createFileSystemResource("测试啊-" + index, ".txt", "中文 2")); + map.put("name", message); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + RestTemplate restTemplate = RestTemplateBuilder.create(); + String result = restTemplate.postForObject("servicecomb://provider/v1/uploadSpringmvcSchema/upload", + new HttpEntity<>(map, headers), String.class); + if (!containsAll(result, "hello1", "中文 2", message)) { + LOGGER.error("failed result is {}, index is ={}", result, index); + failed.set(true); + } + } catch (RestClientException e) { + e.printStackTrace(); + failed.set(true); + } + latch.countDown(); + } + }.start(); + } + + latch.await(); + return "failed=" + failed.get(); + } + + private static boolean containsAll(String str, String... strings) { + for (String string : strings) { + if (!str.contains(string)) { + return false; + } + } + return true; + } +} diff --git a/basic/pom.xml b/basic/pom.xml index bfc4b01..32ab6af 100644 --- a/basic/pom.xml +++ b/basic/pom.xml @@ -27,7 +27,7 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <servicecomb.version>2.1.5</servicecomb.version> + <servicecomb.version>2.3.0-SNAPSHOT</servicecomb.version> </properties> <dependencyManagement> diff --git a/basic/provider/pom.xml b/basic/provider/pom.xml index 846d5a3..bc1b26b 100644 --- a/basic/provider/pom.xml +++ b/basic/provider/pom.xml @@ -35,7 +35,7 @@ <dependencies> <dependency> <groupId>org.apache.servicecomb</groupId> - <artifactId>java-chassis-spring-boot-starter-servlet</artifactId> + <artifactId>java-chassis-spring-boot-starter-standalone</artifactId> </dependency> </dependencies> diff --git a/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java b/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java index b52efe4..6542bce 100644 --- a/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java +++ b/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java @@ -19,14 +19,16 @@ package org.apache.servicecomb.samples; import org.apache.servicecomb.springboot2.starter.EnableServiceComb; import org.springframework.boot.SpringApplication; +import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication @EnableServiceComb public class ProviderApplication { public static void main(String[] args) throws Exception { try { - SpringApplication.run(ProviderApplication.class, args); + new SpringApplicationBuilder().web(WebApplicationType.NONE).sources(ProviderApplication.class).run(args); } catch (Exception e) { e.printStackTrace(); } diff --git a/basic/provider/src/main/java/org/apache/servicecomb/samples/UploadSpringmvcSchema.java b/basic/provider/src/main/java/org/apache/servicecomb/samples/UploadSpringmvcSchema.java new file mode 100644 index 0000000..da5259b --- /dev/null +++ b/basic/provider/src/main/java/org/apache/servicecomb/samples/UploadSpringmvcSchema.java @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.multipart.MultipartFile; + +import com.google.common.collect.Lists; + +@RestSchema(schemaId = "uploadSpringmvcSchema") +@RequestMapping(path = "/v1/uploadSpringmvcSchema") +public class UploadSpringmvcSchema { + private static final Logger LOGGER = LoggerFactory.getLogger(UploadSpringmvcSchema.class); + + @RequestMapping(path = "/upload", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String fileUpload(@RequestPart(name = "file1") MultipartFile file1, + @RequestPart(name = "file2") MultipartFile file2, @RequestAttribute("name") String name) { + return _fileUpload(Lists.newArrayList(file1, file2)) + name; + } + + @RequestMapping(path = "/uploadArray", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String fileUploadArray(@RequestPart(name = "file1") MultipartFile[] file1, + @RequestPart(name = "file2") MultipartFile[] file2, @RequestAttribute("name") String name) { + List<MultipartFile> multipartFileList = new ArrayList<>(); + multipartFileList.addAll(Arrays.asList(file1)); + multipartFileList.addAll(Arrays.asList(file2)); + return _fileUpload(multipartFileList) + name; + } + + @RequestMapping(path = "/uploadList", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String fileUploadList(@RequestPart(name = "file1") List<MultipartFile> file1, + @RequestPart(name = "file2") List<MultipartFile> file2, @RequestAttribute("name") String name) { + file1.addAll(file2); + return _fileUpload(file1) + name; + } + +// @RequestMapping(path = "/uploadArrayList", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) +// public String fileUploadArrayList(@RequestPart(name = "file1") ArrayList<MultipartFile> file1, +// @RequestPart(name = "file2") ArrayList<MultipartFile> file2, @RequestAttribute("name") String name) { +// file1.addAll(file2); +// return _fileUpload(file1) + name; +// } + + @RequestMapping(path = "/uploadWithoutAnnotation", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String uploadWithoutAnnotation(MultipartFile file1, MultipartFile file2, + @RequestAttribute("name") String name) { + return _fileUpload(Lists.newArrayList(file1, file2)) + name; + } + + @RequestMapping(path = "/uploadArrayWithoutAnnotation", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String uploadArrayWithoutAnnotation(MultipartFile[] file1, MultipartFile[] file2, + @RequestAttribute("name") String name) { + List<MultipartFile> multipartFileList = new ArrayList<>(); + multipartFileList.addAll(Arrays.asList(file1)); + multipartFileList.addAll(Arrays.asList(file2)); + return _fileUpload(multipartFileList) + name; + } + + @RequestMapping(path = "/uploadListArrayWithoutAnnotation", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String uploadListWithoutAnnotation(List<MultipartFile> file1, List<MultipartFile> file2, + @RequestAttribute("name") String name) { + file1.addAll(file2); + return _fileUpload(file1) + name; + } + + @RequestMapping(path = "/uploadMix", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public String uploadMix(@RequestPart(name = "file1") List<MultipartFile> file1, + @RequestPart(name = "file2") MultipartFile[] file2, @RequestAttribute("name") String name) { + LOGGER.info("================================================"); + List<MultipartFile> multipartFileList = Arrays.asList(file2); + file1.addAll(multipartFileList); + return _fileUpload(file1) + name; + } + + @RequestMapping(path = "/uploadMultiformMix", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public Map<String, String> uploadMultiformMix(@RequestPart(name = "file") MultipartFile file, + @RequestPart(name = "fileList") List<MultipartFile> fileList, + @RequestPart("str") String str, + @RequestPart("strList") List<String> strList) throws IOException { + LOGGER.info("================================================"); + + HashMap<String, String> map = new HashMap<>(); + map.put("file", new String(file.getBytes(), StandardCharsets.UTF_8.name())); + map.put("fileList", _fileUpload(fileList)); + map.put("str", str); + map.put("strList", strList.toString()); + return map; + } + + private static String _fileUpload(List<MultipartFile> fileList) { + StringBuilder stringBuilder = new StringBuilder(); + try { + for (MultipartFile multipartFile : fileList) { + stringBuilder.append(IOUtils.toString(multipartFile.getBytes(), StandardCharsets.UTF_8.name())); + } + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + return stringBuilder.toString(); + } +}
