Ninja framework endpoint throws 500 error when trying to map JSON to custom object

Cecil Rodriguez :

So I've got a Ninja endpoint here:

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();
}

The class I am trying to map to:

public class RecurOrderJSON {

    public String id;
    public String event_type;
    public String request_id;
    // Maybe switch data type?
    //public Map data;
}

And the route:

router.POST().route("/recurring").with(RecurringController::processRecurring);

I am just trying to send some simple JSON to a webhook and for some reason the object mapping doesn't seem to be working. I think maybe I am misunderstanding the documentation?

http://www.ninjaframework.org/documentation/working_with_json_jsonp.html

The example they give you is this:

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
        ...

    }
}

As far as I can tell, I am doing this correctly? Am I understanding the documentation wrong? Here's an example JSON object - currently I am only trying to grab the top level strings, but I'll eventually want to grab data as well:

{
  "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"
}

Currently I am just trying to get the string values, yet I am constantly getting a 500 error and no other indication in my logs of any error.

As far as I can tell, Ninja should just automatically map the JSON to my object, correct?

Bsquare ℬℬ :

I successfully reproduced your issue, and then fixed it.

First, for easy way to try/test, I recommend (temporary) modifications:

package controllers;

import models.RecurOrderJSON;
import ninja.Context;
import ninja.Result;

public class RecurringController {
    public Result processRecurring(Context context, RecurOrderJSON recurOrderJSON) {
        log.info("recurOrderJSON => " + recurOrderJSON);
        return ninja.Results.ok();
    }
}

And then, update your model this way:

package models;

import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class RecurOrderJSON {

    public String id;
    public String event_type;
    public String request_id;
    public Map data;

    @Override
    public String toString() {
        return "RecurOrderJSON [id=" + id + ", event_type=" + event_type + ", request_id=" + request_id + ", data="
                + data.toString() + "]";
    }
}

You can notice:

  • The data type must stay raw (generic can't be used here)
  • the important @JsonIgnoreProperties(ignoreUnknown = true) annotation to avoid deserialize issue, if ever your source data does not perfectly match your model (be sure to use the recent version of annotation, in fasterxml sub-package, instead of the old one, in codehaus sub-package)
  • the toString() implementation only allowing quick check of OK/KO deserialization

Then you can easily test the system with wget, or curl:

curl -H 'Content-Type: application/json' -d "@/tmp/jsonINput.json" -X POST http://localhost:8080/recurring

Notice it is very important to specify the Content-type for good interpretation.

With the /tmp/jsonINput.json file containing exactly the json contents you specified in your question.

This way, everything is working like a charm, obtaining this output:

recurOrderJSON => RecurOrderJSON [id=hook-XXXXX, event_type=tx-pending, request_id=attempt-XXXXXXX, 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}]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103355&siteId=1