我看到了请求映射方法的几个问题:
理想情况下,您应该@ResponseBody
在方法上有一个注释,以指示要流回返回的内容:
@RequestMapping(value = "index", method = RequestMethod.GET)
@ResponseBody
public HashMap<String, String> handleRequest() {
HashMap<String, String> model = new HashMap<String, String>();
String name = "Hello World";
model.put("greeting", name);
return model;
}
现在完成此操作,您的测试将如下所示:
mockmvc.perform(get("/index").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("application/json"))
.andExpect(jsonPath("$.greeting").value("Hello World"));
在您的情况下,发生的事情是Spring试图找出视图名称,并将视图名称获取为index
,这又是控制器路径,因此就是您所看到的循环视图路径错误。