2013-04-26 72 views
3

我有麻煩後2個參數與RestTemplate:String類型,必需的參數不存在同春RestTemplate

  • 一個String
  • 成MultipartFile

我不認爲這是一個問題在我的控制器中,因爲它非常基礎。看來控制器沒有收到名稱參數。你能告訴我什麼是錯在我的代碼

控制器(接收器)

@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: String類型,必需的參數「名」不存在

+0

您是否嘗試過此版本的postForLocation? '公共URI postForLocation(字符串URL, 對象的要求, 地圖的URLVariables) 拋出RestClientException' – Bizmarck 2013-04-26 14:27:22

+1

一個Spring控制器可以處理文件上傳請求,很容易,只需處理方法?你需要某種解析器,你有嗎? – 2013-04-26 14:27:29

+0

@bizmark我不需要使用'Map urlVariables',因爲我沒有在URL中傳遞任何變量。 – TheEwook 2013-04-26 14:41:38

回答

5

處理多請求是一個複雜的過程。這不像閱讀請求參數那麼簡單。因此,Spring要求您聲明一個MultipartResolver,以便它可以解析和處理這些請求。您可以在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> 

哪裏CommonsMultipartResolver是,解析您的請求並分割部分,使您的控制器可以找到簡單的請求參數和上傳文件(縣)的實施。

相關問題