public class User {
public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {};
@JsonView(UserSimpleView.class) //简单视图 显示用户名
public String getUsername() {
@JsonView(UserDetailView.class) //细节显示密码
public String getPassword() {
此外 在 controller接口上,也需要加入
@GetMapping("/{id:\\d+}")
@JsonView(User.UserDetailView.class)
public User getInfo(@ApiParam("用户id") @PathVariable String id) {
日期处理 传递时间戳
@Test
public void whenUpdateSuccess() throws Exception {
Date date = new Date(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println(date.getTime());
String content = "{\"id\":\"1\", \"username\":\"tom\",\"password\":null,\"birthday\":"+date.getTime()+"}";
String reuslt = 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(reuslt);
}
@PutMapping("/{id:\\d+}")
public User update(@Valid @RequestBody User user, BindingResult errors) {
System.out.println(user.getId());
System.out.println(user.getUsername());
System.out.println(user.getPassword());
System.out.println(user.getBirthday()); //打印出来是 英文的年月日
user.setId("1");
return user; //返回给前台,依然是时间戳
}
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(UserNotExistException.class) //自己的异常
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, Object> handleUserNotExistException(UserNotExistException ex) {
Map<String, Object> result = new HashMap<>();
result.put("id", ex.getId());
result.put("message", ex.getMessage());
return result;
}
}
public class UserNotExistException extends RuntimeException {
private static final long serialVersionUID = -6112780192479692859L;
private String id; //提供get set
public UserNotExistException(String id) {
super("user not exist");
this.id = id;
}
}
可以使用正则@PutMapping(&amp;amp;amp;amp;quot;/{id:\\d+}&amp;amp;amp;amp;quot;)status().is4xxClientError() 参数异常JsonViewpublic class User { public interface UserSimpleView {}; public interface UserDetailView extends UserSimpleView {};