0

I tried to return a list of records as JSON from the controller as follows:

public class ListsController extends APIController  {

    public void index(){
        respond(List.findAll().toJson(true)).contentType("application/json");
    }
}

ApiController is defines as follows:

public abstract class APIController extends AppController {
    @Override
    protected String getContentType() {
        return "application/json";
    }

    @Override
    protected String getLayout() {
        return null;
    }

}

When I test it:

@Test
    public void shouldRenderLists(){
        app.models.List list = new app.models.List();
        list.set("id", 1);
        list.set("code", "some code");
        list.set("name", "some name");
        list.set("item_code", "some item code");
        list.saveIt();
        request().integrateViews().get("index");

        the(contentType()).shouldBeEqual("application/json");
        Map[] listsMaps = JsonHelper.toMaps(responseContent());

        the(listsMaps.length).shouldBeEqual(1);
        Map listEntry = listsMaps[0];

        the(listEntry.get("code")).shouldBeEqual("some code");
        the(listEntry.get("name")).shouldBeEqual("some name");
    }

It fails because responseContent() returns an [\n\n]. Any ideas why so ?

belgoros
  • 3,590
  • 7
  • 38
  • 76
  • your code looks fine. Can you try to print the same from controller and see if you are actually sending an empty string somehow? – ipolevoy Dec 04 '17 at 16:21
  • Yes, I have an empty String in the controller as well.```public void index(){ HttpBuilder responseBuilder = respond(List.findAll().toJson(true)); }``` – belgoros Dec 04 '17 at 16:53
  • Then you need to find out why, grab data from db `List.findAll().dump()` – ipolevoy Dec 04 '17 at 16:55
  • you can simplify your code: ```app.models.List list = new app.models.List().createIt("id", 1, "code", "some code", "name", "some name", "item_code", "some item code"); request().get("index");``` – ipolevoy Dec 04 '17 at 17:08
  • also, I suggest to not set ID, database will do it automatically – ipolevoy Dec 04 '17 at 17:08
  • If I don't set id, it fails: `org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint`. The value of `json` passed in to `public static Map[] toMaps(String json)` is still an empty array: `[\n\n]`. – belgoros Dec 04 '17 at 18:03
  • here is an example of DDL for PostgreSQL: https://github.com/javalite/activejdbc/blob/master/activejdbc/src/test/resources/postgres_schema.sql. Create a temporary repo on Github with your code, I will help then – ipolevoy Dec 04 '17 at 18:19
  • @ipolevoy, Thank you in advance. I run the DDL you provided and created a [git repo](https://github.com/belgoros/newton-parameter). Nothing works any more, really weird :(: PeopleControllerSpec fails. – belgoros Dec 04 '17 at 20:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160488/discussion-between-belgoros-and-ipolevoy). – belgoros Dec 05 '17 at 09:12

0 Answers0