【发布时间】:2019-03-21 10:14:34
【问题描述】:
所以我在这里有一个 Ninja 端点:
public Result processRecurring(Context context, RecurOrderJSON recurOrderJSON) {
String id = recurOrderJSON.id;
String event_type = recurOrderJSON.event_type;
String request_id = recurOrderJSON.request_id;
//Map data = recurOrderJSON.data;
//recurringRouter(event_type, data);
log.info("ID value");
log.info(id);
return JsonResponse.build()
.message("OK")
.toResult();
}
我要映射到的类:
public class RecurOrderJSON {
public String id;
public String event_type;
public String request_id;
// Maybe switch data type?
//public Map data;
}
还有路线:
router.POST().route("/recurring").with(RecurringController::processRecurring);
我只是想向 webhook 发送一些简单的 JSON,但由于某种原因,对象映射似乎不起作用。我想我可能误解了文档?
http://www.ninjaframework.org/documentation/working_with_json_jsonp.html
他们给你的例子是这样的:
If you send that JSON to your application via the HTTP body you only need to add the POJO class to the controller method and Ninja will parse the incoming JSON for you:
package controllers;
public class ApplicationController {
public Result parsePerson(Person person) {
String nameOfPerson = person.name; // will be John Johnson
...
}
}
据我所知,我这样做对吗?我对文档的理解有误吗?这是一个示例 JSON 对象 - 目前我只是想抓取顶级字符串,但我最终也想抓取数据:
{
"id": "hook-XXXXX",
"event_type": "tx-pending",
"data": {
"button_id": "static",
"publisher_organization": "org-XXXXXXX",
"campaign_id": "camp-097714a40aaf8965",
"currency": "USD",
"order_currency": "USD",
"id": "tx-XXXXXXX",
"category": "new-user-order",
"modified_date": "2018-10-15T05:41:12.577Z",
"order_total": 9680,
"button_order_id": "btnorder-77c9e56fd990f127",
"publisher_customer_id": "XymEz8GO2M",
"rate_card_id": "ratecard-41480b2a6b1196a7",
"advertising_id": null,
"event_date": "2018-10-15T05:41:06Z",
"status": "pending",
"pub_ref": null,
"account_id": "acc-4b17f5a014d0de1a",
"btn_ref": "srctok-0adf9e958510b3f1",
"order_id": null,
"posting_rule_id": null,
"order_line_items": [
{
"identifier": "Antique Trading Card",
"description": "Includes Lifetime Warranty",
"amount": 9680,
"publisher_commission": 968,
"attributes": {},
"total": 9680,
"quantity": 1
}
],
"order_click_channel": "webview",
"order_purchase_date": null,
"validated_date": null,
"amount": 968,
"customer_order_id": null,
"created_date": "2018-10-15T05:41:12.577Z",
"commerce_organization": "org-XXXXXX"
},
"request_id": "attempt-XXXXXXX"
}
目前我只是试图获取字符串值,但我不断收到 500 错误,并且在我的日志中没有其他任何错误指示。
据我所知,Ninja 应该只是自动将 JSON 映射到我的对象,对吗?
【问题讨论】:
-
JsonResponse 的全称是什么?这是一个自定义类还是来自某个扩展?我没有看到它列在ninjaframework.org/apidocs/index.html
-
还有一个问题。您确定收到的是 500 Internal Server Error 而不是 400 Bad Request?您使用什么工具发送请求?
-
你能确认请求到达控制器吗?既然你提到了 500 错误。
-
先把它分解成一个非常简单的例子(例如“hello world”类型的例子)——然后从那里开始构建
标签: java json ninjaframework