我试图从控制器返回一个记录列表作为 JSON,如下所示:
public class ListsController extends APIController {
public void index(){
respond(List.findAll().toJson(true)).contentType("application/json");
}
}
ApiController 定义如下:
public abstract class APIController extends AppController {
@Override
protected String getContentType() {
return "application/json";
}
@Override
protected String getLayout() {
return null;
}
}
当我测试它时:
@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");
}
它失败了,因为responseContent()
返回一个 [ \n\n
]。任何想法为什么会这样?