繁体   English   中英

MultipartFile + String 作为请求参数在 Spring 中使用 RestTemplate

[英]MultipartFile + String as Request Param using RestTemplate in Spring

在我的 Spring Boot 应用程序中,我需要将 String 值与 MultipartFile 作为Requestparam一起Requestparam 控制器在下面,我将MultipartFile转换为java.io.File ,然后在restTemplate帮助下将其传递给 DAO 控制器。

从角请求将先打上传控制器然后UploadController是客户端(JAVA),这将是调用服务器SVS-BA-道控制器,基本URL。 csvUpload包含基本 URL:http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=multipartFile

@CrossOrigin
@RestController
public class UploadController {

    private static final Logger log = LogManager.getLogger(UploadController.class);

    @Value("${salama.dao.csvUpload.rest.url}")
    private String csvUpload;
    
    @Autowired
    UploadHelperService uploadHelperService;
    
    @PostMapping(value = "/csvUpload")
    public String csvUpload(@QueryParam("parentPkId") String parentPkId, @RequestParam("file") MultipartFile file ) throws IllegalStateException, IOException{
        
        log.info("Calling csvUpload :" + csvUpload);
        final HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

        File cnvFile = uploadHelperService.multipartToFile(file,file.getOriginalFilename());
        System.out.println("Converted File Name is -->"+cnvFile.getName());
        final MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
        
        data.add("parentPkId", parentPkId);
        data.add("file", new FileSystemResource(cnvFile));
        
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data, requestHeaders);
    
        RestTemplate restTemplate = new RestTemplate();
        
        ResponseEntity<String> obj =restTemplate.exchange(csvUpload, HttpMethod.POST,
            requestEntity, String.class);
        
        return obj.getBody();
}
    
    
}

在 svs-ba-dao Controller 中捕获它,如下所示

@RequestMapping(value="/csvUpload", method=RequestMethod.POST)
    public String csvUpload(@RequestParam String parentPkId, @RequestParam MultipartFile file) throws IOException {
        log.info("Entered method csvUpload() of svs-ba-dao.class");
        return uploadService.csvUpload(parentPkId,file);
    }

我已将这些属性包含到我的应用程序的 application.properties 文件中: spring.servlet.multipart.maxFileSize=1MB spring.servlet.multipart.maxRequestSize=1MB

所以我启动我的应用程序并调用生成 POST 请求的/csvUpload 我得到低于错误。

Converted File Name is -->testInput_TxnMpMotSlv.csv
2019-01-24T15:12:41,217 ERROR [[localhost].[/svs-ba-dao].[dispatcherServlet]] [http-nio-8085-exec-2] Servlet.service() for servlet [dispatcherServlet] in context with path [/svs-ba-dao] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException: 500 null] with root cause
org.springframework.web.client.HttpServerErrorException: 500 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at com.svs-ba-dao.controller.UploadController.csvUpload(UploadController.java:59) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_192]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_192]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_192]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_192]

错误在 resttemplate 中,因为它采用 null,但在语法上调用 RestTemplate 的方式是正确的,因为我没有收到任何错误。 我是否正确调用了基本 URL ?通过传递requestparam值。 即 parentPkId=?&file=multipartFile

http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=multipartFile
老实说,我以前没有做过这方面的工作。 我做错的地方,欢迎任何线索或更正。 提前致谢

在这里我找到了我错的地方

csvUpload 将保存应该在 rest 模板中传递的 URL

 @Value("${salama.dao.csvUpload.rest.url}")
        private String csvUpload;

我发现基本 URL:即 csvUpload http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=multipartFileUploadController发送是错误的。

无需在 URL 中为mediaType指定multipartFilemediaType 它会自动接听。 但是对于某些 URL,如果发送 JSON 对象,则需要指定?mediaType=json

这是我更新的基本URLhttp://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=

打电话是没有问题的。 仔细查看您的堆栈跟踪错误。 在 csvUpload 方法中,您打印:

System.out.println("Converted File Name is -->"+cnvFile.getName());

这一行打印在日志中:

Converted File Name is -->testInput_TxnMpMotSlv.csv

所以打电话没问题。 问题在这里:

at com.svs-ba-dao.controller.UploadController.csvUpload(UploadController.java:59)

就在UploadControllercsvUpload方法来检查的行号59。 你会发现你的错误。

看看这个 如何使用rest模板调用multipart

您可以做一些事情来了解实际发生的事情:

  1. 尝试从svs-ba-dao Controller调用svs-ba-dao Controller中的svs-ba-dao Controller端点,看看它是否独立工作。 另外,将您的 csvUpload 方法更新为

    @RequestMapping(value="/csvUpload", method=RequestMethod.POST) public String csvUpload(@RequestParam("parentPkId") String parentPkId, @RequestParam("file") MultipartFile file) throws IOException { log.info("Entered method csvUpload() of svs-ba-dao.class"); return uploadService.csvUpload(parentPkId,file); }

  2. 环绕您的 RestTemplate 调用以try-catch

    ResponseEntity<String> obj =restTemplate.exchange(csvUpload, HttpMethod.POST, requestEntity, String.class);

为了查看来自svs-ba-dao Controller的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM