@RequestBody
,当前端使用ajax,传输json数据的时候,只需要在实体的 Date属性上加 @JsonFormat
即可,因为springboot默认使用Jackson处理json//controller @RequestMapping("/save") @ResponseBody public String save(HttpServletRequest request, @RequestBody DutyBean dutyBean){ return null; } //bean import java.util.Date; import com.fasterxml.jackson.annotation.JsonFormat; public class DutyBean implements Serializable { private static final long serialVersionUID = -5999151785938349105L; /** * 事件名称 */ private String dutyName; /** * 创建人 */ private Long createUser; /** * 创建时间 */ @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss") private Date createTime; /** * 最后修改人 */ private Long lastModifyUser; /** * 最后修改时间 */ private Date lastModifyTime; /** * 是否完结 */ private Integer isEnd; /** * 提醒时间 */ private Date remindTime; /** * 是否提醒(1否,0是) */ private Integer isRemind; /** * 日志内容 */ private String dutyLog; /** * 文件集合 */ private List<DutyFileModel> fileList; //省略get set }
contentType: 'application/json;charset=utf-8', data:JSON.stringify(json),
@Entity @Table(name = "t_p5_blog") @ToString public class Blog { @Id @GeneratedValue private Long bid; @Column(length = 256) private String title; @Column(length = 512) private String summary; @Column private Date releaseDate; @Column private Date handleTime; @Column private Long clickHit; @Column private Long replyHit; @Column private String content; @Column(length = 256) private String url; @Column(length = 4) private String status; @Column(length = 64) private String source; @Column(length = 64) private String typeName;
/** * 针对时间String转Date * @param dataBinder */ @InitBinder public void InitBinder(WebDataBinder dataBinder) { dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { public void setAsText(String value) { try { setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value)); } catch(ParseException e) { setValue(null); } } public String getAsText() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) getValue()); } }); }
@RequestMapping("/save") public String save(Blog blog, HttpServletRequest request){ blog.setReleaseDate(blog.getReleaseDate() == null ? new Date() : blog.getReleaseDate()); this.blogService.save(blog); return "redirect:/admin/blog/list"; }
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有