@JsonFormat注解和@DateTimeFormat注解异同点

Posted life is wonderful

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@JsonFormat注解和@DateTimeFormat注解异同点相关的知识,希望对你有一定的参考价值。

 

@JsonFormat注解和@DateTimeFormat注解异同点

 

基础知识:

HTTP请求和响应:key-value、请求体、Content-Type

Spring注解:@RequestParam、@RequestBody@ResponseBody

 

@JsonFormat注解

情况一:

1、使用LocalDateTime

2、post请求且请求体数据是json格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@JsonFormat(pattern = Constant.DATE_TIME_FORMAT)
	LocalDateTime registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(@RequestBody TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

 

情况二:

1、使用LocalDateTime

2、post请求且请求数据是键值对格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@JsonFormat(pattern = Constant.DATE_TIME_FORMAT)
	LocalDateTime registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(@RequestBody TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

报错信息:

情况三:

1、使用Date

2、post请求且请求体数据是json格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@JsonFormat(pattern = Constant.DATE_TIME_FORMAT)
	Date registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(@RequestBody TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

情况四:

1、使用Date

2、post请求且请求数据是键值对格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@JsonFormat(pattern = Constant.DATE_TIME_FORMAT)
	Date registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

报错信息

结论:

@DateTimeFormat注解

1、能格式化前端传入的日期时间类型的参数,也能格式化后端传给前端的日期时间类字段

2、仅支持Json格式的请求提交方式,不仅支持前端使用表单键值对的请求提交方式

3、适用java.util.Date和 java.time.LocalDateTime

4、适用get请求和post请求

 

@DateTimeFormat注解

情况一:

1、使用LocalDateTime

2、post请求且请求体数据是json格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.time.LocalDateTime;

import org.springframework.format.annotation.DateTimeFormat;

import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@DateTimeFormat(pattern = Constant.DATE_TIME_FORMAT)
	LocalDateTime registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(@RequestBody TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

报错信息

情况二:

1、使用Date

2、post请求且请求体数据是json格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@DateTimeFormat(pattern = Constant.DATE_TIME_FORMAT)
	Date registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(@RequestBody TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

报错信息

情况三:

1、使用Date

2、post请求且请求数据是键值对格式,返回值也为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@DateTimeFormat(pattern = Constant.DATE_TIME_FORMAT)
	Date registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

情况四:

1、使用LocalDateTime

2、post请求且请求数据是键值对格式,返回值为json格式

TestParam源码

package com.huajin.registerserver.controller;

import java.io.Serializable;
import java.time.LocalDateTime;

import org.springframework.format.annotation.DateTimeFormat;

import com.huajin.common.util.Constant;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TestParam implements Serializable 
	
	static final long serialVersionUID = 2754207548498337771L;
	
	@DateTimeFormat(pattern = Constant.DATE_TIME_FORMAT)
	LocalDateTime registerTime;

TestController源码

package com.huajin.registerserver.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huajin.common.domain.DataResponse;
import com.huajin.registerclient.common.enums.sys.ErrorCode;
import com.huajin.registerserver.annotation.WithoutLogin;

@RestController
@RequestMapping("/test")
public class TestController 
	
	@WithoutLogin
	@PostMapping("/test")
	public DataResponse<TestParam> test(TestParam testParam) 
		return DataResponse.<TestParam>create()
				                          .setRetcode(ErrorCode.SUCCESS.getCode())
		                                  .setMsg(ErrorCode.SUCCESS.getMessage())
                                          .setData(testParam);
	

Postman请求

结论:

@DateTimeFormat注解

1、只能格式化前端传入的日期时间类型的参数,不能格式化后端传给前端的日期时间类字段

2、仅支持前端使用表单键值对的请求提交方式,不支持Json格式的请求提交方式

3、适用java.util.Date和 java.time.LocalDateTime

4、适用get请求和post请求

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

以上是关于@JsonFormat注解和@DateTimeFormat注解异同点的主要内容,如果未能解决你的问题,请参考以下文章

@JsonFormat注解和@DateTimeFormat注解异同点

@JsonFormat注解和@DateTimeFormat注解异同点

@DateTimeFormat和@JsonFormat注解

json之jackson的注解@Jsonformat

页面输出格式常用注解----@JsonIgnore,@JsonFormat,@JsonFormat

Spring Boot使用@JsonProperty,@JsonIgnore,@JsonFormat注解