Search code examples
javaninjaframework

Ninja framework unable to parse form into Java object


I am just becoming familiar with the Ninja framework. I am experimenting with parsing forms into Java objects. However, though this appears to follow the documentation, it does not appear to be working.

route:

router.GET().route("/create_user").with(UserController.class, "createUser");

Controller:

    public Result createUser(Context context, UserTest userTest) {
        System.out.println(userTest);
        return Results.text().renderRaw("success");
    }

UserTest: public class UserTest { private String name; private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserTest [name=" + name + ", age=" + age + "]";
    }

}

Input: http://localhost:8080/create_user?name=test&age=5 Output: Though "success" is correctly returned to the browser, the UserTest object is null.

Any thoughts?

Oh and I just noticed this message: @qtp-1661406123-0] DEBUG ninja.utils.AbstractContext - Not able to parse body because request did not send content type header at: /create_user


Solution

  • After a bit of testing I figured out that the object parsing is not triggered for GET routes. The code should work fine given a route specified by router.POST()... instead of router.GET()...