【发布时间】:2020-05-06 07:53:07
【问题描述】:
我有以下 API 定义:
@ResponseBody
@PostMapping(path = "/configureSection")
public ResponseEntity<ResultData> configureSegment(@RequestParam() String Section,
@RequestBody SectionConfig sectionConfig);
public class SegmentConfig {
@JsonProperty(value = "txConfig", required = true)
TelemetryConfig telemetryConfig;
@JsonProperty(value = "rxConfig")
ExternalConfig externalConfig;
}
我定义了 txConfig 是必需的属性(即类),但是当发送空 JSON ({}) 时,spring boot 仍然运行 API,而我希望它返回参数 tsConfig 未设置的错误。
知道我错过了什么吗?
编辑:使用@Valid 和@NotNull 解决了它,但返回的错误信息量太大,有什么方法可以解决它?
{
"timestamp": 1588753367913,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"NotNull.segmentConfig.telemetryConfig",
"NotNull.telemetryConfig",
"NotNull.services.TelemetryConfig",
"NotNull"
],
"arguments": [
{
"codes": [
"segmentConfig.telemetryConfig",
"telemetryConfig"
],
"arguments": null,
"defaultMessage": "telemetryConfig",
"code": "telemetryConfig"
}
],
"defaultMessage": "Please provide a valid txConfig",
"objectName": "segmentConfig",
"field": "telemetryConfig",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Bad Request",
"path": "/mypath"
}`
【问题讨论】:
-
尝试在
telemetryConfig字段上添加@Valid注释
标签: java spring-boot jackson