您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

java.lang.AssertionError:junit Spring MVC Controller时未设置内容类型吗?

java.lang.AssertionError:junit Spring MVC Controller时未设置内容类型吗?

我看到了请求映射方法的几个问题:

理想情况下,您应该@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,这又是控制器路径,因此就是您所看到的循环视图路径错误

java 2022/1/1 18:15:07 有662人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶