笔头云 笔头云
首页
设计模式
SQL教程
Redis
归档
关于
友链

笔头云

非淡泊无以明志,非宁静无以致远。
首页
设计模式
SQL教程
Redis
归档
关于
友链
  • JsonProperty
    • JsonProperty
    • JsonProperty.Access
    • 示例代码
  • Git常用命令
  • Office Util办公工具
  • FRP内网穿透docker部署
  • 后端
笔头云
2023-12-25
目录

JsonProperty

# JsonProperty

用在属性上,将属性名称序列化为另一个名称。

# 全路径
com.fasterxml.jackson.annotation.JsonProperty

# 使用
@JsonProperty("name")
private String productName;

# 依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

1
2
3
4
5
6
7
8
9
10
11
12
13

注意

通过url传参时,JsonProperty注解不生效,需要用原字段接收参数。

# JsonProperty.Access

JsonProperty.Access.WRITE_ONLY: 只反序列化(读取此字段),序列化时忽略此字段(接口不返回此字段), 如: password(密码),salt(加盐)等。
JsonProperty.Access.READ_ONLY: 只序列化(接口返回此字段),反序列化时忽略此字段(不读取此字段), 如: id等。

注意

JsonIgnore: 在序列化和反序列化时忽略字段,导致接收不到字段的值,如果需要接收字段的值,可以使用JsonProperty.Access.WRITE_ONLY注解。

# 示例代码

    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.Data;
    
    import java.io.Serializable;
    
    @Data
    @TableName("tb_order")
    public class Order implements Serializable {
    
      private static final long serialVersionUID = -1012949090969825707L;
    
      @TableId(type = IdType.AUTO)
      private Long id;
      private String orderNo;
      // 只反序列化(读取此字段),序列化时忽略此字段(接口不返回此字段)
      @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
      private String userId;
      // 只序列化(接口返回此字段),反序列化时忽略此字段(不读取此字段)
      @JsonProperty(access = JsonProperty.Access.READ_ONLY)
      private int amount;
      
      @JsonProperty("name")
      private String productName;
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    @Slf4j
    @RestController
    @RequestMapping(value = "/api/serial")
    public class SerialController {
    
      @Resource
      private OrderService orderService;
    
      // @JsonProperty("name")
      // api/serial/order/body {"name":"product1"}
      @GetMapping(value = "/orders/body")
      public ResponseEntity<?> tetPost(@RequestBody Order order) {
        log.info("获取订单,请求体传参:{}", order);
        return ResponseEntity.ok(orderService.selectList());
      }
    
      // url传参@JsonProperty("name")不生效,使用原属性名
      // api/serial/order/url?productName=product2
      @GetMapping(value = "/orders/url")
      public ResponseEntity<?> tetGet(Order order) {
        log.info("获取订单,URL传参:{}", order);
        return ResponseEntity.ok(orderService.selectList());
      }
    
      // JsonProperty.Access.READ_ONLY修饰的字段,反序列化时被忽略
      @PostMapping(value = "/orders")
      public ResponseEntity<?> create(@RequestBody Order order) {
        log.info("创建订单:{}", order);
        return ResponseEntity.ok(orderService.createOrder(order));
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    // Make sure to add code blocks to your code group
    上次更新: 2023/12/25, 16:19:04
    Git常用命令

    Git常用命令→

    最近更新
    01
    FRP内网穿透docker部署 工具
    05-07
    02
    Office Util办公工具 工具
    01-14
    03
    Git常用命令
    01-16
    更多文章>
    Theme by Vdoing | Copyright © 2023-2025 鲁ICP备2023014898号 公安备案号:37020302372159
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×