1

Two things are going wrong here, which might be two problems or might have the same root: they both feel like a config error somewhere, hence putting them together. Apologies is advance if this is confusing the issue...

I'm using Ninja Framework, still learning my way around starting from the Hello World example. I can serve pages with GET and POST using Routes, a Controller and html OK.

In case it is relevant, I'm running on a fresh CentOS VM, using NetBeans to write, Firefox to test. I have restarted Ninja (many times).

Problem 1: NinjaProperties isn't visible in code. I had understood that I can put, for example

import com.google.inject.Inject;
public abstract class PersistDB {
    @Inject
    NinjaProperties ninjaProperties;
    ...
    protected void loadProps() {
        dbReadServer = ninjaProperties.getWithDefault(DB_READ_SERVER_PROP, DBSERVER_DEFAULT);
        ...
    }
}

public class UsersDB {
    public UsersDB() {
       loadProps();
    }
}

and have my model code access the application.conf file to extract properties. However ninjaProperties is coming back null.

Problem 2: The JSON parsing described at Ninja docs where adding a simple class with the right field names to the controller signature caused JSON to be translated behind the scenes isn't working. I can add the class, with the right fields, to the Controller method signature but it is always null. Using GSON I can extract the object for myself so I have a work-around but that also confirms that it is something in Ninja that isn't working.

The code follows the example pretty closely:

package controllers;

public class UserRegisterReq {
  String email;
  String identName;
  String password;

}

and

package controllers;

import com.google.inject.Singleton;
import models.user.User;
import models.user.UserResult;
import ninja.Context;
import ninja.Result;
import ninja.Results;
import ninja.session.Session;

@Singleton
public class UserController {

...

public Result doRegisterPost(UserRegisterReq req, Context context)  {
    Session session = context.getSession();
    UserResult ur;

    ur = User.registerUser(req.email, req.password, req.identName);
    ...
}

Any ideas? Thanks in advance...

4

1 回答 1

2

without code is really hard to say why your app is not working. Both things should just work out of the box and so work on production applications and in our integration tests perfectly fine.

Some ideas regarding 1) Here is a working example: https://github.com/ninjaframework/ninja/blob/develop/ninja-servlet-integration-test/src/main/java/controllers/ApplicationController.java

As you can see NinjaProperties is injected and is working there. Just compare that to your code... It should be straight forward to fix.

Based on your note it is pretty clear what is going on. Your are using "new". If you use "new" you'll break any injection framework. If you @Inject your UsersDB into your controller and then execute the methods on the exectued usersDb it will work. Either use new and don't use @Inject inside those classes. Or use @Inject, but also @Inject the very class where you inject. That's something related how injection frameworks under Java work (and in particular Guice).

Some ideas regarding 2)

Here again some code that works and parses Json: https://github.com/ninjaframework/ninja/blob/develop/ninja-servlet-integration-test/src/main/java/controllers/PersonController.java

Should also work out of the box. One thing though - you have to make sure that the request contains header content-type: application/json - otherwise Ninja cannot know how to parse the incoming request.

I am sure this will be easy to fix. Let us know if that helped!

于 2014-07-15T18:30:58.243 回答