spring boot jackson 时间转换

Entity

@Data
public class ExampleLeave  implements Serializable {
    private String id;
    private String title;
    private String leaveType;
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date startDate;
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date endDate;
    private BigDecimal count;
    private String reason;
    private int status;
}

工具类

public class JsonResult implements Serializable {
  

    public static <T> T convertValue(String content, Class<T> toValueType ) {
        ObjectMapper mapper = objectMapper();
        mapper.setDateFormat(new SimpleDateFormat(DateFormat.DEFAULT_PATTERN.getValue()));
        try {
            return mapper.readValue(content, toValueType);
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.toString());
            return null;
        }
    }
    private static ObjectMapper objectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return mapper;
    }
}

Controller

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public void save(String  leave) {
       ExampleLeave exampleLeave= JsonResult.convertValue(leave,ExampleLeave.class);
    }

猜你喜欢

转载自blog.csdn.net/xintingandzhouyang/article/details/97795444