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 = "";
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM