2013-04-02 63 views
1

我在使用Spring restTemplate時遇到了一些問題。在春天的寧靜服務導致400差的請求

根據我試圖訪問的Rest服務的API文檔(僅適用於PHP)。 POST的正確格式爲:

curl -d "data={\"client_email\":\"[email protected]\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}" http://www.setster.com/api/v2/company/7089/appointment?session_token=niab4ptf9mjjem41cooso389f3 

來獲取響應:

{ 
"statusCode":0, 
"statusDescription":"OK", 
"data":{ 
    "status":2, 
    "client_id":103352, 
    "client_email":"[email protected]", 
    "client_name":"Steve Ryner", 
    "company_id":"7089", 
    "employee_id":9862, 
    "location_id":"13832", 
    "start_date":"2012-08-15 14:00", 
    "end_date":"2012-08-15 15:00", 
    "length":3600000, 
    "note":"This is a test.", 
    "service_id":20216, 
    "type":"60 Min Swedish", 
    "duration_padding":0, 
    "repeat_type":0, 
    "subservices":"", 
    "timezone_dif":-18000, 
    "price":"60", 
    "custom_fields":"[]", 
    "client_phone":"", 
    "client_address":"", 
    "payment_pending":0, 
    "id":171302483 
} 

}

這裏是我的代碼的重要組成部分:

RestTemplate restTemplate = new RestTemplate(); 

Map<String, String> data = new HashMap<String, String>(); 

     data.put("client_name", "Alexandre Moraes"); 
     data.put("client_email", "[email protected]"); 
     data.put("client_phone", "98065867"); 
     data.put("employee_id", "0"); 
     data.put("location_id", "16675"); // Here i have to specify the "Location_ID" because there is more than just one 
     data.put("start_date", "2013-05-03 09:15:00"); 
     data.put("service_id", "18499"); 

     String result = restTemplate.postForObject("http://setster.com/api/v2/company/6788/appointment/?session_token="+session_token, data, String.class); 

     resp.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = resp.getWriter(); 
     out.println(result); 

但是,當我執行那postForObject,響應是一個400錯誤的請求好像有什麼東西送錯了:

WARNING: POST request for "http://setster.com/api/v2/company/6788/appointment/?session_token=[censored]" resulted in 400 (Bad Request); invoking error handler 
WARNING: StandardWrapperValve[setster]: PWC1406: Servlet.service() for servlet setster threw exception 

也許格式我會發送數據是錯誤的。但我無法弄清楚如何管理這些信息。

任何人都知道我做錯了什麼?

回答

0

使用傑克遜LIB - 是對象轉換爲JSON簡單的方法:

ObjectMapper mapper = new ObjectMapper();

mapper.writeValue(resp.getWriter(), data); 
+2

對「restTemplate.postForObject」的調用不起作用,不是響應。 –

1

在你捲曲的要求,你的HTTP POST的主體爲:

data={\"client_email\":\"[email protected]\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"} 

所以您的端點需要一個帶有稱爲data的單個字符串的標準HTTP POST,以及您的序列化JSON。 RestTemplate假設您要以這種方式提交:

{\"client_email\":\"[email protected]\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"} 

即只是一個JSON對象。你將無法使用RestTemplate。對於HTTP服務使用Spring's built-ins之一。

+0

感謝@ jordan002提供的有用信息,實際上這是我第一次看到Java,並且有些事情難以理解:** 1。 「Spring內置的Http服務」意味着什麼?**對不起。我的英語也不太好,所以你能舉個例子嗎?謝謝。 – Alexandre

+0

添加了一個指向Spring遠程處理手冊的鏈接。希望這會有所幫助。如果您對回覆感到滿意,那麼將會非常感激接受或讚揚。 –