我的应用程序中有一个接受日志的控制器。当我发送一个空的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”,或者拒绝验证中的对象?
1条答案
按热度按时间izkcnapc1#
正如@tushar在评论中提到的,将logdto对象中的类型从基本类型更改为 Package 器解决了我的问题。