问题
i tried to read the content of the json with jsonPath and i get an error.
Here the junit test method:
mockMvc.perform(get("/path")
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(1)))
.andExpect(jsonPath("$.name", is("NAME")))
.andReturn().getResponse().getContentAsString();
here is what the request return me:
[{"id":1,"name":"NAME","....}, ....}]
i got this error:
No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes.
can someone helps me.
Thanks
回答1:
The response returns a JSON array and using "$.id"
you try to access the id
property of that array. This - as the error message tells you - is not possible.
Test the id
and name
property on the first element of the array instead:
.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].name", is("NAME")))
来源:https://stackoverflow.com/questions/35251927/jsonpath-no-value-for-json-path-id-exception-path-id-is-being-applied-to