忽略 spring boot api 请求体中的空字段

Ignoring empty fields in spring boot api requestbody

我的应用程序中有一个接受日志的控制器。当我发送一个空的 json 对象('{}')或有效请求但具有一个或多个空字段时,它会自动反序列化为一个空的 LogDTO 对象或一个字段设置为 0 的 LogDTO(对于数字字段).我想拒绝空字段的请求。

我的控制器:

@PostMapping("new/log")
public ResponseEntity<Log> newLog(@Valid @RequestBody LogDTO logDTO) {
    return new ResponseEntity<>(logService.newLog(logDTO), HttpStatus.OK);
}

LogDTO 对象:

public class LogDTO {

/**
 * The date and time for this specific log, in miliseconds since epoch.
 */
@Min(0)
@NotNull
@JsonInclude(JsonInclude.Include.NON_NULL)
private long epochDate;

/**
 * The heartRate per minute for this specific time.
 */
@Min(0)
@NotNull
@JsonInclude(JsonInclude.Include.NON_NULL)
private int heartRate;

/**
 * The user this log belongs to.
 */
@Min(0)
@NotNull
@JsonInclude(JsonInclude.Include.NON_NULL)
private long userId;

/**
 * The night this log belongs to. Every sleepsession represents one night.
 */
@Min(0)
@NotNull
@JsonInclude(JsonInclude.Include.NON_NULL)
private long sleepSession;

public LogDTO() {
}

public LogDTO(long epochDate, int heartRate, long userId, long sleepSession) {
    this.epochDate = epochDate;
    this.heartRate = heartRate;
    this.userId = userId;
    this.sleepSession = sleepSession;
}
//getters and setters

我也尝试在我的应用程序属性中设置 'spring.jackson.default-property-inclusion=non-default',但它一直将字段设置为“0”。有什么方法可以将空字段设置为 'null' 而不是“0”,或者在验证中拒绝该对象?

正如@Tushar 在评论中提到的,将我在 LogDTO 对象中的类型从原始类型更改为包装器解决了我的问题。