@JsonProperty注解解析 偏执的太偏执、 2023-10-01 12:43 4阅读 0赞 ### 1. 概述 ### 来源: @JsonPrpperty是jackson包下的一个注解,详细路径 (com.fasterxml.jackson.annotation.JsonProperty;) 作用: @JsonProperty用在属性上,将属性名称序列化为另一个名称。 例子: public class Person{ @JsonProperty(value = "name") private String realName; } 拓展:jackson可以理解为java对象和json对象进行转化的工具包。 ### 2. 实例 ### 解释:定义一个实体类,定义一个Controller层,通过post请求传入body,分别验证加和不加@JsonProperty注解的区别。 2.1 引入依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> 2.2 创建实体类 package com.gxn.demo.domain; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Objects; public class Person { // 先将该注解注释掉 // @JsonProperty(value = "name") private String realName; private Integer age; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(realName, person.realName) && Objects.equals(age, person.age); } @Override public int hashCode() { return Objects.hash(realName, age); } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } 2.3 定义Controller,返回结果是realName和age的值 package com.gxn.demo.controller; import com.gxn.demo.domain.Person; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class PersonController { @PostMapping("/name") public String getName(@RequestBody Person person) { String name = person.getRealName(); Integer age = person.getAge(); String res = name + " : " + age; return res; } } 2.4 验证结果 * 未加注解,body中json的key和实体属性一致: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70] * 未加注解,body中json的key和实体属性不一致: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 1] * 加注解,body中json的key和实体属性一致: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 2] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 3] * 加注解,body中json的key与注解的value一致:: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 4] ### 3. 总结 ### 通过@JsonProperty注解可以改变实体对应属性名,一旦使用该注解我们在body中传参数的就需要按照注解中value的值来定义key。 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70]: https://img-blog.csdnimg.cn/20210518234646992.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 1]: https://img-blog.csdnimg.cn/20210518234731327.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 2]: https://img-blog.csdnimg.cn/20210518234913411.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 3]: https://img-blog.csdnimg.cn/20210518234941709.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u_size_16_color_FFFFFF_t_70 4]: https://img-blog.csdnimg.cn/20210518235012901.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dfeF9u,size_16,color_FFFFFF,t_70
还没有评论,来说两句吧...