在Spring Rest中,我有一个RestController公开此方法:
@RestController
@RequestMapping("/controllerPath")
public class MyController{
@RequestMapping(method = RequestMethod.POST)
public void create(@RequestParameter("myParam") Map<String, String> myMap) {
//do something
}
}
我想使用Spring中的MockMVC测试此方法:
// Initialize the map
Map<String, String> myMap = init();
// JSONify the map
ObjectMapper mapper = new ObjectMapper();
String jsonMap = mapper.writeValueAsString(myMap);
// Perform the REST call
mockMvc.perform(post("/controllerPath")
.param("myParam", jsonMap)
.andExpect(status().isOk());
问题是我收到500 HTTP 错误代码。我很确定这是因为我使用Map作为控制器的参数(我尝试将其更改为 String 并且它有效)。
问题是:如何在我的RestController中有一个Map in 参数,并使用MockMVC正确测试它?
谢谢你的帮助。