2014-02-12 59 views
0

我嘗試在駱駝中使用restlet。該請求應該是一個JSON。但不要如何將JSON轉換爲java對象。這裏是代碼示例:camel restlet Json整合問題

客戶端請求:

$.ajax({ 
    url: '${pageContext.request.contextPath}/rs/search?restletMethod=POST', 
type: "POST", 
data: $('#searchRequest').serializeArray(), 
contentType: "application/json;charset=utf-8", 
dataType: "html", 
success: function (result) 
{ 
$("#result #queryId").text(result); 
}, 
error: function (xhr, ajaxOptions, thrownError) { 
alert(xhr.status); 
alert(thrownError.Message); 
} 
}); 

RouterBuilder:

from("restlet:/search?restletMethod=POST").unmarshal("gson") 
    .to("bean:restletHandler"); 

camelcontext.xml

<bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat">  
    <property name="unmarshalType" value="domain.SearchRequest"/> 
</bean> 

我一直得到以下情況除外:

C om.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期BEGIN_OBJECT但STRING在第1分1列

Exchange中的身體是org.restlet.engine.io.UnclosableInputStream 的類型是這樣的:

性別=女& _sex =上&性別=男性& _sex =上& _sex =上& _race =上&種族=黑色& _race =上&種族=印度& _race =上& _race =上& _race =上&性別= &藥物= 2345 & _medication =上& _medication =上& _medication =上& _medication =上& _medication =上& _diagnosis =上&診斷= 410.90 & _diagnosis =上& _diagnosis =上& _diagnosis =在& _diagnosis =上&描述= sdfdsfQuery RequestcloseID描述請求創建日期所有者狀態完成日期計數

身體流並不像JSON,所以gson不知道如何將其轉換爲對象。 任何幫助將真正感激!

感謝,

回答

0

使用JSON.parse()來和內容類型必須設置爲application/JSON

+0

感謝您的答覆!當我傳遞數據爲:{「demographics.sex」:[「female」,「male」,「unknown」],「demographics.race」:「indian」,「medications.includeCodes」:[「2345」,「 4567「],」medications.excludeCodes「:」4567「,」diagnostic.includeCodes「:[」250.00「,」410.90「,」401.9「],」description「:」cxzcxzc「,」gender「:」「} 在「gson」之後創建的對象缺少字符串數組: –