我发送put请求一直报400怎么回事
来源:3-5 修改和删除请求
BestLater
2018-03-08
@Test
public void whenUpdateUser() throws Exception {
Date date = new Date(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
String content = "{\"id\":1,\"password\":\"123456\",\"age\":25,\"birthday\":"+date+"}";
String str = mockMvc.perform(put("/user/1")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andReturn().getResponse().getContentAsString();
System.out.println(str);
}
controller:
@PutMapping("/{id:\\d+}")
@JsonView(User.UserDiteleView.class)
public User updateUser(@RequestBody @Valid User user, BindingResult error) {
if (error.hasErrors()) {
error.getAllErrors().forEach(err -> System.out.println(err.getDefaultMessage()));
}
return user;
}写回答
1回答
-
BestLater
提问者
2018-03-08
找到怎么回事了,因为前段传的是字符串,不能直接把date对象放进去,代码改成
String content = "{\"id\":1,\"password\":\"123456\",\"age\":25,\"birthday\":"+date.getTime()+"}";
10
Spring Security技术栈开发企业级认证与授权
Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全
2662 学习 · 1561 问题
相似问题