標準的な方法でデータを返す方法を見つけようとしています。これが意味することは、json または xml を返すときに、すべて (成功とエラー) に対して 1 つの形式があればよいということです。
次のjson結果があるとします。
{
"person": {
"id": 12345,
"firstName": "John",
"lastName": "Doe",
"phones": {
"home": "800-123-4567",
"work": "888-555-0000",
"cell": "877-123-1234"
},
"email": [
"jd@example.com",
"jd@example.org"
],
"dateOfBirth": "1980-01-02T00:00:00.000Z",
"registered": true,
"emergencyContacts": [
{
"name": "",
"phone": "",
"email": "",
"relationship": "spouse|parent|child|other"
}
]
}
}
これで問題ありませんが、検証エラーが発生した場合はどうなりますか
組み込みメソッド CreateErrorResponse を使用できます
{
"Message": "The request is invalid.",
"ModelState": {
"item": [
"Required property 'Name' not found in JSON. Path '', line 1, position 14."
],
"item.Name": [
"The Name field is required."
],
"item.Price": [
"The field Price must be between 0 and 999."
]
}
}
*はい、データが意味をなさず、異なっていることは知っていますが、データは構造だけでは意味がありません。
エラーが発生した場合はどうなるでしょうか。この場合、カスタム エラー コードがあります。
このようなものを返すことができました(HttpErrorを使用)
{
"Message": "My custom error message",
"CustomErrorCode": 37
}
これで、3 つの異なる形式の json が戻ってくることがわかります。今クライアントで私はこれをしなければならないでしょう
- HttpStatusCode を確認する
- 200 の場合、この場合は Person の形式を使用して json を解析します。
- 400 の場合は、検証エラーまたはサーバー エラーである可能性があります。
- カスタマー エラーが見つかった場合は、その形式を使用します。それ以外の場合は、modlestate を使用します。
私はフォースクエアで作業しており、常に同じ形式をユーザーに返すようですが、同じようなものを取得する方法がわかりません。
{
"meta": {
"code": 200,
...errorType and errorDetail...
},
"notifications": {
...notifications...
},
"response": {
...results...
}
}
私はそれに似たようなことをしたいと思います
リクエストはOKでしょう。
{
"meta": {
"code": 200,
"ModelState": {}
},
"response": {
"person": {
"id": 12345,
"firstName": "John",
"lastName": "Doe",
"phones": {
"home": "800-123-4567",
"work": "888-555-0000",
"cell": "877-123-1234"
},
"email": [
"jd@example.com",
"jd@example.org"
],
"dateOfBirth": "1980-01-02T00:00:00.000Z",
"registered": true,
"emergencyContacts": [
{
"name": "",
"phone": "",
"email": "",
"relationship": "spouse|parent|child|other"
}
]
}
}
}
サーバーエラーは次のようになります
{
"meta": {
"code": 500,
"message": "this is a server error",
"ModelState": {}
},
"response": {}
}
検証は次のようになります
{
"meta": {
"code": 400,
"message": "validation errors",
"Message": "The request is invalid.",
"ModelState": {
"item": [
"Required property 'Name' not found in JSON. Path '', line 1, position 14."
],
"item.Name": [
"The Name field is required."
],
"item.Price": [
"The field Price must be between 0 and 999."
]
}
},
"response": {}
}
しかし、私が言ったように、このようなことを行う方法がわからず、これが最善の方法であると100%確信しているわけではありません. 少なくともそれは1つのフォーマットであるべきですか?
@エリック・フィリップスを編集
asp.net mvc プロジェクトだけを行っていたときは、このようなことをしていました。
public readonly IValidation validation;
public PersonService(IValidation validation)
{
this.validation = validation;
}
public Person GetPerson(int id)
{
try
{
return FindPerson(id);
}
catch(Exception ex)
{
//log real error with elmah
validation.addError("internal", "Something went wrong");
}
}
public class PersonController
{
public readonly IPersonService personService;
public PersonController(IPersonService personService)
{
this.personService = personService;
}
public ActionResult GetPerson(int id)
{
personService.GetPerson(id);
if(personService.Validation.IsValid)
{
// do something
}
else
{
// do something else
}
return View();
}
}
設定方法は気に入っていますが、このままにしておきたいと思います。インターフェースは使えないと思いますが、こんなものを考えていました
public PersonService()
{
}
public ResponseResult<Person> GetPerson(int id)
{
var result = ResponseResult<Person>();
try
{
return FindPerson(id);
}
catch(Exception ex)
{
result.Errorcode = 200;
result.Msg = "Failed";
}
}
public class PersonController
{
public readonly IPersonService personService;
public PersonController(IPersonService personService)
{
this.personService = personService;
}
public HttpResponseMessage GetPerson(int id)
{
var result = personService.GetPerson(id);
if(result.isValid)
{
Request.CreateResponse<ResponseResult<Person>>(HttpStatusCode.OK, result);
}
Request.CreateResponse<ResponseResult<Person>>(HttpStatusCode.BadRequest, result);
}
}