RestTemplate的异常:Not enough variables available to expand

时间:2023-03-09 00:49:29
RestTemplate的异常:Not enough variables available to expand

原因:RestTemplate使用出错,我的情况是不知道这里要求用RestTemplate的使用格式,应该很多人都是这样吧?不过,看了下RestTemplate,感觉其实还是很好用的。

RestTemplate是一种使用格式,正确的格式为

mockMvc.perform(MockMvcRequestBuilders
.post("/lookRecord/delete/{collectId}", "8")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
直接传值
mockMvc.perform(MockMvcRequestBuilders
.post("/lookRecord/delete/8")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();

错误的格式,不要使用转义符

mockMvc.perform(MockMvcRequestBuilders
.post("/lookRecord/delete/{\"collectId\"}","8")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();

不要忘记传值

 mockMvc.perform(MockMvcRequestBuilders
.post("/lookRecord/delete/{collectId}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();

错误的直接传值

 mockMvc.perform(MockMvcRequestBuilders
.post("/lookRecord/delete/{8}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();

简易理解的方式为

String data= {"\"name\":\"jack\",\"age\":18"};
String url = "http://localhost:8080/search?people={data}";
mockMvc.perform(MockMvcRequestBuilders
.post(url,data)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();