Java Spring RestTemplate中不存在必需的字符串参数

Java Spring RestTemplate中不存在必需的字符串参数,java,spring,rest,spring-mvc,resttemplate,Java,Spring,Rest,Spring Mvc,Resttemplate,我无法使用RestTemplate发布2个参数: 一串 多部分文件 我认为我的控制器没有问题,因为它非常基本。控制器似乎没有收到name参数。你能告诉我我的密码出了什么问题吗 控制器(接收器) Rest客户端(发送方) RestTemplate rest=new RestTemplate(); URI=新的URI(“http://127.0.0.1:7011/xxxxxxxx/admin/fileupload"); MultiValueMap parts=新链接的MultiValueMap(

我无法使用RestTemplate发布2个参数:

  • 一串
  • 多部分文件
我认为我的控制器没有问题,因为它非常基本。控制器似乎没有收到name参数。你能告诉我我的密码出了什么问题吗

控制器(接收器)

Rest客户端(发送方)

RestTemplate rest=new RestTemplate();
URI=新的URI(“http://127.0.0.1:7011/xxxxxxxx/admin/fileupload");
MultiValueMap parts=新链接的MultiValueMap();
添加(“名称”、“导入密钥”);
资源文件=新类路径资源(“xmlFileImport/file.xml”);
部分。添加(“文件”,文件);
rest.postForLocation(uri,部分);
控制器堆栈跟踪

org.springframework.web.bind.MissingServletRequestParameterException: 所需的字符串参数“name”不存在


处理多部分请求是一个复杂的过程。它不像读取请求参数那么简单。因此,Spring要求您声明一个,以便它能够解析和处理此类请求。您可以在
applicationContext.xml
文件中执行此操作:

<bean id="multipartResolver"  
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <property name="maxUploadSize">  
        <value> <YOUR_SIZE> </value>  
    </property>  
    <property name="maxInMemorySize">  
        <value> <YOUR_SIZE> </value>  
    </property>      
</bean>


解析您的请求并拆分部分的实现在哪里,以便您的控制器可以找到普通请求参数和上载的文件。

您是否尝试过此版本的postForLocation
PublicURI postForLocation(字符串url、对象请求、映射url变量)抛出RestClientException
Spring控制器能否仅使用处理程序方法轻松处理多部分请求?你需要某种解析器,你有吗?@bizmark我不需要使用
映射URL变量
,因为我不在URL中传递任何变量。@SotiriosDelimanolis你是对的!!我没有使用任何解析器。我只是使用CommonsMultipartResolver,它可以工作。把你的评论作为回答,我会接受的。Thanks@TheEwook我很高兴你明白了。检查我的答案是否包含所有细节。如果没有,您可以添加自己的答案以供将来参考。
RestTemplate rest = new RestTemplate();
URI uri = new URI("http://127.0.0.1:7011/xxxxxxxx/admin/fileupload");

MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name", "import_keys");
Resource file = new ClassPathResource("xmlFileImport/file.xml");
parts.add("file", file);

rest.postForLocation(uri, parts);
<bean id="multipartResolver"  
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <property name="maxUploadSize">  
        <value> <YOUR_SIZE> </value>  
    </property>  
    <property name="maxInMemorySize">  
        <value> <YOUR_SIZE> </value>  
    </property>      
</bean>