异常:com.fasterxml.jackson.core.JsonParseException:无法识别的令牌"代码":预期("真"、"假"或"空")



com.fasterxml.jackson.core.JsonParseException: 无法识别的令牌"代码":是预期的("真"、"假"或"空"( 在 [来源: [B@5ff2e84b; 行: 1, 列: 9]

我在运行模拟测试用例时收到此错误。

请找到以下代码以获取更多信息。

@Test
public void testFindLearningStandardItemCodeById() throws Exception{
String id = "2411";
String json = JsonUtil.getInstance().writeValueAsString(id);
String learningStandardItemCode = new String("code");
when(learningStandardItemManager.findCodeById(anyString())).thenReturn(learningStandardItemCode);
ResultActions resultActions = mockMvc.perform(post("/standards/jurisdiction/standard/item/findCodeById")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(json.getBytes())
);
resultActions.andExpect(status().isOk());
final MvcResult mvcResult = resultActions.andReturn();
String contents = mvcResult.getResponse().getContentAsString();
JsonResponse response = JsonUtil.getInstance().getObjectFromJsonResponse(contents, JsonResponse.class);
System.out.print(response);
assertNotNull(response);
}

谢谢 Mandar DharurKar,

但是我找到了解决方案,我们只需要传递字符串而不是解析的字符串。 例如accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(id)

call content(String str( 而不是 content(byte[] content(& 当我们在那个时候检索响应时,调用 getObjectFromResponse(( 方法而不是 getObjectFromJsonResponse((。

例如String response = JsonUtil.getInstance().getObjectFromResponse(contents, String.class);

它对我有用..!

相关内容

最新更新