我正在使用 Spring MVC 开发 REST 服务,该服务将对象列表作为请求参数。
@RequestMapping(value="/test", method=RequestMethod.PUT)
public String updateActiveStatus(ArrayList<Test> testList, BindingResult result) throws Exception {
if(testList.isEmpty()) {
throw new BadRequestException();
}
return null;
}
当我尝试对上述服务进行集成测试时,我无法在请求参数中发送测试对象列表。
以下代码对我不起作用。
List<Test> testList = Arrays.asList(new Test(), new Test());
mockMvc.perform(put(ApplicationConstants.UPDATE_ACTIVE_STATUS)
.content(objectMapper.writeValueAsString(testList)))
.andDo(print());
任何人都可以帮忙解决这个问题吗?
最佳答案
@RequestParam with List or array
@RequestMapping("/books")
public String books(@RequestParam List<String> authors,
Model model){
model.addAttribute("authors", authors);
return "books.jsp";
}
@Test
public void whenMultipleParameters_thenList() throws Exception {
this.mockMvc.perform(get("/books")
.param("authors", "martin")
.param("authors", "tolkien")
)
.andExpect(status().isOk())
.andExpect(model().attribute("authors", contains("martin","tolkien")));
}
关于spring-mvc - 使用对象列表作为请求参数的 MockMvc 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56488012/