我还找不到该问题的解决方案...基本上我想测试一些产生 json 响应的其余资源,json 以安全字符串为前缀,例如:
"qwerty{"status":"success"}"
当然,前缀这不是一个有效的 json,因此 json 路径失败:
mockMvc.perform(get("/myresource")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("$.status").value("success"))
出现以下错误:
java.lang.IllegalArgumentException: 在 org.springframework.test 的 com.jayway.jsonpath.JsonPath.read(JsonPath.java:211) 的 com.jayway.jsonpath.JsonPath.read(JsonPath.java:180) 处的容器对象无效。 util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:98) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:115) at org.springframework.test.web.servlet.result.JsonPathResultMatchers$2.match(JsonPathResultMatchers .java:68) 在 org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:152)
有没有办法跳过给定的前缀并让 jsonpath 正常工作?也许使用全局配置?
编辑以澄清情况,在杰克逊 http 消息转换器中声明了前缀:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
converter.setPrefixJson(true);
converter.setJsonPrefix("qwerty");
return converter;
}
在此处编辑相关 jira 问题的链接