我正在尝试使用RestSharp的Execute方法来查询rest端点并将其序列化为POCO的非常简单的示例。然而,我尝试的所有内容都导致响应.Data对象具有NULL值的所有属性。
这是JSON响应:
这是我的测试代码。
最后,这是我的POCO。
此外,响应中的ErrorException和ErrorResponse属性为NULL。
这似乎是一个非常简单的情况,但我已经困扰了一整天!谢谢。
这是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
时会发生什么? - David Hoerster