Java 忽略spring启动api请求体中的空字段

Java 忽略spring启动api请求体中的空字段,java,spring-boot,jackson,Java,Spring Boot,Jackson,我的应用程序中有一个接受日志的控制器。当我发送一个空的json对象(“{}”)或有效请求,但有一个或多个空字段时,它会自动反序列化为一个空的LogDTO对象或一个字段设置为0(对于数字字段)的LogDTO。我想拒绝带有空字段的请求 我的控制器: @PostMapping("new/log") public ResponseEntity<Log> newLog(@Valid @RequestBody LogDTO logDTO) { return new R

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

我的控制器:

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

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

如注释中提到的@Tushar,将我的日志中的类型从基本类型更改为对象解决了我的问题。

如注释中提到的@Tushar,将我的LogDTO对象中的类型从基元类型更改为包装器解决了我的问题。

是否可以更改为包装器类型而不是基元?整数而不是int,Long而不是Long看看这个:@Tushar我可以,经过一些重构。这对我来说会有什么变化?当你使用原语时,它默认为零而不是空。我尝试过使用包装器类型,您会得到一个验证异常。@Tushar谢谢,这很有效。您能改为包装器类型而不是原语吗?整数而不是int,Long而不是Long看看这个:@Tushar我可以,经过一些重构。这对我来说会有什么变化?当你使用原语时,它默认为零而不是空。我试过了,使用包装器类型,您会得到一个验证异常。@Tushar谢谢,这很有效。
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