3

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?

4
  • What is the fully qualified name of JsonResponse? Is this a custom class or from some extension? I don't see it listed at ninjaframework.org/apidocs/index.html
    – anttix
    Oct 19, 2018 at 4:20
  • One more question. Are you sure you're getting a 500 Internal Server Error and not 400 Bad Request? What tool do you use to send the request with?
    – anttix
    Oct 19, 2018 at 4:37
  • Can you confirm that the request is coming to the controller ? Since you mentioned a 500 error. Oct 23, 2018 at 2:52
  • Break it down into a very simple example first (e.g. a "hello world" type example) - and build up from there
    – PaulS
    Oct 23, 2018 at 12:49

4 Answers 4

1
+200

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}]
1

Given the specific input code with data field commented out

//public Map data;

and the posted input JSON that includes this field, the request should fail with 400 Bad Request.

The reason being that Ninja uses Jackson for JSON parsing and it will throw on unknown fields by default.

The quick workaround is to add @JsonIgnoreProperties annotation to RecurOrderJSON class.

e.g.

@JsonIgnoreProperties(ignoreUnknown = true)
public class RecurOrderJSON {
    ...
}

See: Ignoring new fields on JSON objects using Jackson

Now if the error was not 400 there isn't much information to go by as there doesn't seem to be anything else obviously wrong with the code.

Either post an SSCCE demonstrating the problem or attempt to debug by surfacing the error page with the following method:

  1. Launch the application in debug mode with mvn package ninja:run
  2. Access the end-point with a tool that allows to inspect the response in detail such as curl e.g.
    1. Store request JSON in input.json
    2. Run curl -v -o result.html -H 'Content-Type: application/json' --data '@input.json' http://localhost:8080/recurring
    3. Open result.html to examine the response
0

Might it be that you are performing a bad request (hence the JSON is not found) but for some Ninja bug it returns error 500?

For example you can take a look here where is stated that parsing an empty JSON in a JSON request does leads to a misguiding error (500) while it is supposed to return 400 "Bad Request"

0

Context not needed in processRecurring and use Results.json() and return original

public Result processRecurring(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 Results.json().render(recurOrderJSON);
}

Make sure you get the namespace in your RecurOrderJSON

package models;

public class RecurOrderJSON {

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

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.