@JsonProperty 前端传参数名和后端参数名不一样 入参 出参 映射 注解 springboot springmvc

04-23 1372阅读 0评论

省流:

使用 @JsonProperty

public class Product{
    @JsonProperty("productName")
    private String name;
}

前端传productName,后端使用 @JsonProperty 转换一下。

返回也是一样。如果出参也用这个对象,出参用的是 @JsonProperty 里的字段名。前端拿到的是productName。

一、解读 

@JsonProperty 位于 com.fasterxml.jackson.annotation 

依赖如下 

 
    com.fasterxml.jackson.core 
        jackson-databind 
    2.5.3
    

常用到jackson的注解:

@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat

1、@JsonProperty

常用写法:@JsonProperty("xxx"),一般标记在字段上。指定入参和出参的字段名。

2、@JsonIgnore

一般标记在字段或者方法上,返回的json数据即不包含该字段。

3、@JsonIgnoreProperties

类注解,作用是json序列化时将java bean中的一些字段忽略掉,序列化和反序列化都受影响。

4、@JsonFormat

用于字段或者方法上(最好是字段上),将字符串的时间转换成Date类型。需要前端提交的是json格式数据,即ajax提交。

写法:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")

二、案例

 

@Data
@JsonIgnoreProperties(value = { "entryDate", "leaveDate" })
public class Emp implements Serializable {
	private static final long serialVersionUID = 1L;
 
	@JsonProperty("empName")
	private String name;
 
	@JsonIgnore
	private int age;
 
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")
	private Date birthday;
 
	private String deptNo;
 
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")
	private Date entryDate;
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")
	private Date leaveDate;
}

三、测试

测试需要用到 new ObjectMapper() 里的 writeValueAsString,readValue,这是fasterxml 里自己的,很方便。

new ObjectMapper().writeValueAsString()

new ObjectMapper().readValue()

		String empJsonStr= new ObjectMapper().writeValueAsString(emp);
		System.out.println(empJsonStr);
				
		Emp emp = new ObjectMapper().readValue(empJsonStr, Emp.class);
		System.out.println(emp);
		Emp emp = new Emp();
		emp.setAge(11);
		emp.setBirthday(new Date());
		emp.setDeptNo("0011");
		emp.setEntryDate(new Date());
		emp.setLeaveDate(new Date());
		emp.setName("张三");
		// 将对象转换成json字符串
		String stuJson = new ObjectMapper().writeValueAsString(emp);
		System.out.println(stuJson);
//打印  {"birthday":"2023-07-07 12:03:58","deptNo":"0011","empName":"张三"}
		
		//将字符串转换成对象
		String s = "{\"entryDate\":\"2023-07-07 11:49:1\",\"age\":\"11\",\"birthday\":\"2023-07-07 11:49:14\",\"deptNo\":\"0011\",\"empName\":\"张三\"}";
		Emp stu = new ObjectMapper().readValue(s, Emp.class);
		System.out.println(stu.toString());
//打印  Emp(name=张三, age=0, birthday=Fri Jul 07 19:49:14 CST 2023, deptNo=0011, entryDate=null, leaveDate=null)

注意empName,age,entryDate,leaveDate

====================分割线=======================

文章到此已经结束,以下是紫薯布丁

public class Product{

    @JsonProperty("productName")

    private String name;

}

 

    com.fasterxml.jackson.core 

        jackson-databind 

    2.5.3

 

@Data

@JsonIgnoreProperties(value = { "entryDate", "leaveDate" })

public class Emp implements Serializable {

    private static final long serialVersionUID = 1L;

 

    @JsonProperty("empName")

    private String name;

 

    @JsonIgnore

    private int age;

 

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")

    private Date birthday;

 

    private String deptNo;

 

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")

    private Date entryDate;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")

    private Date leaveDate;

}

         String empJsonStr= new ObjectMapper().writeValueAsString(emp);

        System.out.println(empJsonStr);

        

        

        Emp emp = new ObjectMapper().readValue(empJsonStr, Emp.class);

        System.out.println(emp);

        Emp emp = new Emp();

        emp.setAge(11);

        emp.setBirthday(new Date());

        emp.setDeptNo("0011");

        emp.setEntryDate(new Date());

        emp.setLeaveDate(new Date());

        emp.setName("张三");

        // 将对象转换成json字符串

        String stuJson = new ObjectMapper().writeValueAsString(emp);

        System.out.println(stuJson);

//打印  {"birthday":"2023-07-07 12:03:58","deptNo":"0011","empName":"张三"}

        

        //将字符串转换成对象

        String s = "{\"entryDate\":\"2023-07-07 11:49:1\",\"age\":\"11\",\"birthday\":\"2023-07-07 11:49:14\",\"deptNo\":\"0011\",\"empName\":\"张三\"}";

        Emp stu = new ObjectMapper().readValue(s, Emp.class);

        System.out.println(stu.toString());

//打印  Emp(name=张三, age=0, birthday=Fri Jul 07 19:49:14 CST 2023, deptNo=0011, entryDate=null, leaveDate=null)

 


免责声明
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明。
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所
提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何
损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在
转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并白负版权等法律责任。

手机扫描二维码访问

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1372人围观)

还没有评论,来说两句吧...

目录[+]