我試圖做一個使用RestSharp的Execute方法來查詢一個剩餘端點並序列化到一個POCO的非常簡單的例子。然而,我嘗試的所有東西都會產生一個response.Data對象,它具有所有帶NULL值的屬性。當我反序列化JSON響應時,RestSharp客戶端返回所有屬性爲空
這裏是JSON響應:
{
"Result":
{
"Location":
{
"BusinessUnit": "BTA",
"BusinessUnitName": "CASINO",
"LocationId": "4070",
"LocationCode": "ZBTA",
"LocationName": "Name of Casino"
}
}
}
這裏是我的測試代碼
[TestMethod]
public void TestLocationsGetById()
{
//given
var request = new RestRequest();
request.Resource = serviceEndpoint + "/{singleItemTestId}";
request.Method = Method.GET;
request.AddHeader("accept", Configuration.JSONContentType);
request.RootElement = "Location";
request.AddParameter("singleItemTestId", singleItemTestId, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
//when
Location location = api.Execute<Location>(request);
//then
Assert.IsNotNull(location.LocationId); //fails - all properties are returned null
}
這裏是我的API代碼
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = Configuration.ESBRestBaseURL;
//request.OnBeforeDeserialization = resp => { resp.ContentLength = 761; };
var response = client.Execute<T>(request);
return response.Data;
}
最後,這裏是我的POCO
public class Location
{
public string BusinessUnit { get; set; }
public string BusinessUnitName { get; set; }
public string LocationId { get; set; }
public string LocationCode { get; set; }
public string LocationName { get; set; }
}
此外,響應中的ErrorException和ErrorResponse屬性爲NULL。
這似乎是一個非常簡單的情況,但我一直在圈子裏跑來跑去!謝謝。
當你調用'request.AddUrlSegment(「singleItemTestId」,singleItemTestId)'而不是你必須調用'AddParameter'時會發生什麼? –