2014-02-09 71 views
0

這是我的代碼如何設置restTemplate參數類型?

RestTemplate restTemplate = new RestTemplate(); 

    MultiValueMap<String, Object> vars = new LinkedMultiValueMap<String, Object>(); 
    vars.add("name", fileName); 
    vars.add("parent", "43"); 
    vars.add("size_s3", size_s3); 
    vars.add("size_ssg", size_ssg); 
    vars.add("size", size); 

    HttpHeaders headers = new HttpHeaders(); 
    headers.set("Authorization","token "+user.getToken()); 
    headers.setContentType(MediaType.APPLICATION_JSON); 

    HttpEntity<?> entity = new HttpEntity<MultiValueMap<String, Object>>(vars, headers); 
    result = restTemplate.exchange(RestInfo.restURL+"/files", HttpMethod.POST, entity, String.class).getBody(); 

的httpClientErrorException是400 BAD REQUEST

{ 「size_s3」:[ 「輸入一個整數。」], 「父」:[「不正確的類型預期pK值,收到名單。「],」大小「:[」輸入一個整數。「]}

我認爲prameter類型的問題。所以我試圖改變我的代碼,像這樣

vars.add(「size_s3」,Integer.parseInt(size_s3));

,但結果是一樣的...

如何設置restTemplate參數類型?

幫我看看!

回答

0
RestTemplate restTemplate = new RestTemplate(); 

MultiValueMap<String, Object> vars = new LinkedMultiValueMap<String, Object>(); 
vars.add("name", fileName); 
vars.add("parent", 43); 
vars.add("size_s3", size_s3); 
vars.add("size_ssg", size_ssg); 
vars.add("size", size); 

HttpHeaders headers = new HttpHeaders(); 
headers.set("Authorization","token "+user.getToken()); 
headers.setContentType(MediaType.APPLICATION_JSON); 

HttpEntity<?> entity = new HttpEntity<Object>(vars, headers); 
result = restTemplate.exchange(RestInfo.restURL+"/files", HttpMethod.POST, entity, String.class).getBody(); 

我會在你的代碼的一些變化試試