1. 程式人生 > >dto 返回 null 的處理 (必須或非必須欄位控制)

dto 返回 null 的處理 (必須或非必須欄位控制)

1. 如果要dto 上不顯示 null 值的直段那麼使用註解
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderDTO {
    //todo
}

2. 配置全域性的設定可以在yml 配置檔案

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 
    url: jdbc:mysql://localhost:3306/sell?serverTimezone=GMT%2B8&characterEncoding=utf-8&userSSL=false
jpa: show-sql: true jackson: default-property-inclusion: non_null

3.要欄位必須返回

public class OrderDTO {

    private String orderId;
    private String buyerName;
    private String buyerPhone = "";
}