我在使用 RestTemplate 发布 2 个参数时遇到问题:
我不认为我的控制器有问题,因为它非常基本。看来控制器没有收到名称参数。你能告诉我我的代码有什么问题吗
控制器(收件人)
@RequestMapping(value="/fileupload", method=RequestMethod.POST)
public void handleFileUpload(@RequestParam("name") String fileUploadHandlerName,
@RequestParam("file") MultipartFile file)
{
[...]
}
其余客户端(发件人)
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);
控制器堆栈跟踪
org.springframework.web.bind.MissingServletRequestParameterException:
所需的字符串参数“名称”不存在
处理多部分请求是一个复杂的过程。它并不像读取请求参数那么简单。因此,Spring 要求你声明一个MultipartResolver http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/multipart/MultipartResolver.html以便它可以解析和处理此类请求。您可以在您的applicationContext.xml
file:
<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>
Where CommonsMultipartResolver http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/multipart/commons/CommonsMultipartResolver.html是解析您的请求并拆分各个部分的实现,以便您的控制器可以找到纯请求参数和上传的文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)