简体   繁体   English

无法使用Spring RestTemplate将混合了MultiPartFile的POST参数发送到Rest Service

[英]Unable to send POST parameters with mix of MultiPartFile to Rest Service using spring RestTemplate

I have Spring Rest service defined as below. 我有如下定义的Spring Rest服务。

@RequestMapping(value = "/mobilebuild", method = RequestMethod.POST)
    public StringWrapper buildApp(@RequestParam("projectName") String projectName, @RequestParam("appId") String projectId, @RequestParam("version") String version, @RequestParam("app") MultipartFile file) {
        //Process to build app
        return WMUtils.SUCCESS_RESPONSE;
    }

From client side i am using rest template as follows 从客户端,我正在使用其余模板,如下所示

final List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter(Charset.forName(CommonConstants.UTF8)));
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<Source>());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());

RestTemplate template = new RestTemplate(messageConverters);

MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
 //Post Parameters
parts.add("projectName", "FirstProject");
parts.add("appId", "app12345");
parts.add("version", "1.0");   
// MultipartFile
parts.add("app", new FileSystemResource(tempFilesStorageManager.getFilePath("/tmp/app.zip")));  

HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", auth);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

String url = "http://localhost:8080/AppManager/services/mobilebuild";

HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(parts, headers);
ResponseEntity<String> responseEntity = template.postForEntity(endpointAddress, requestEntity, String.class);
String response = responseEntity.getBody();

I am unable to read the request parameters from controller (server): getting the following error 我无法从控制器(服务器)读取请求参数:收到以下错误

Error: request parameter projectName is not present in the request. 错误:请求中没有请求参数projectName

So please suggest me the way to achieve this. 因此,请向我建议实现此目标的方法。

According to javadoc of HttpEntity , the first parameter is request body and second one is request headers, but your client is sending request parameters inside request body and your controller is expecting them as @RequestParam , hence the error. 根据HttpEntity javadoc ,第一个参数是请求正文,第二个参数是请求标头,但是您的客户HttpEntity在请求正文中发送请求参数,并且您的控制器将其期望为@RequestParam ,因此出错。

So either change your client to send the request parameters in the end point address URL to match your server side as ...projectName=FirstProject&appId= app12345&version=1.0.... 因此,要么更改您的客户端以在端点地址URL中发送请求参数,以将您的服务器端匹配为...projectName=FirstProject&appId= app12345&version=1.0....

Or encapsulate all your @RequestParam fields inside a single DTO class and add @RequestBody annotation on server side if your client wants to send in request body. 或将所有@RequestParam字段封装在一个DTO类中,如果客户端要发送请求正文,则在服务器端添加@RequestBody批注。

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

相关问题 无法使用RestTemplate和Spring Data REST发布关系的新实体 - Unable to post new entity with relationship using RestTemplate and Spring Data REST 如何使用RestTemplate调用MultipartFile Spring REST URL - How to call MultipartFile Spring REST URL with RestTemplate MultipartFile + String 作为请求参数在 Spring 中使用 RestTemplate - MultipartFile + String as Request Param using RestTemplate in Spring 使用Spring RestTemplate消耗REST服务 - Consume REST Service using Spring RestTemplate 无法通过RestTemplate将文件上传到Spring REST服务 - Unable to upload a file to Spring REST service via RestTemplate SPRING:无法使用rest模板在发布请求中传递参数 - SPRING: Unable to pass parameters in a post request using rest template REST POST 与 POSTMAN 一起正常工作,但在使用 Spring RestTemplate 时出现异常 - REST POST works correctly with POSTMAN but exception when using Spring RestTemplate Spring RestTemplate post 在 HashMap 中使用参数会抛出 400 Bad Request - Spring RestTemplate post using parameters in a HashMap throws 400 Bad Request 使用 spring restTemplate 发送 post 请求时出现异常 - have an exception when using spring restTemplate to send a post request 使用Spring Boot的Spring Rest:将MultipartFile和Json对象作为参数上传 - Spring rest with Spring Boot: Upload MultipartFile and Json object as parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM