我正在使用Spring框架並試圖發佈帖子請求。我的post方法使用url,HttpMethod和進入請求主體的參數。Spring RESTtemplate POST
注意:在network.POSTRequest(URL,..,...)中傳遞的var URL對於每個調用都是不同的。現在
,如果我把這種方法,象這樣一個參數......(它精美的作品!)
//with one parameter
MultiValueMap<String, String> postParams = new LinkedMultiValueMap<String, String>();
postParams.add("id", "524cd432539ed");
network.POSTRequest(URL, HttpMethod.POST, postParams);
,但如果有像這樣兩個參數....(它拋出這個錯誤)
org.springframework.web.client.HttpServerErrorException:500內部服務器錯誤
//with two parameters
MultiValueMap<String, String> postParams = new LinkedMultiValueMap<String, String>();
postParams.add("id", "crisp");
postParams.add("name", "honey");
network.POSTRequest(URL, HttpMethod.POST, postParams);
這是POSTRequest方法
public Object POSTRequest(String URL, HttpMethod method, MultiValueMap<String, String> postParams){
HttpEntity<?> requestEntity =
new HttpEntity< MultiValueMap<String, String> >(postParams, getHeaders());
RestTemplate restTemplate = new RestTemplate();
List< HttpMessageConverter<?> > messageConverters = new ArrayList< HttpMessageConverter<?> >();
messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
System.out.println(requestEntity.toString());
ResponseEntity result =
restTemplate.exchange(URL, method, requestEntity, APIResponse.class) ;
return result.getBody();
}
讓我們來看看 –
你有測試發表您的位指示端點手動使用瀏覽器POST插件或'wget'或'curl'使用單個或多個參數?它可能是終點是在2個參數上嘔吐。 – nickdos
是的,我已經做了一個使用郵遞員(鉻擴展)的請求,它是成功的。 –