【发布时间】:2020-05-06 20:02:19
【问题描述】:
我有以下 REST API:
@ResponseBody
@PostMapping(path = "/configureSegment")
public ResponseEntity<ResultData> configureSegment(@RequestParam() String segment,
@RequestBody @Valid CloudSegmentConfig segmentConfig
) {
CloudSegmentConfig:
@JsonProperty(value="txConfig", required = true)
@NotNull(message="Please provide a valid txConfig")
TelemetryConfig telemetryConfig;
@JsonProperty(value="rxConfig")
ExternalSourcePpdkConfig externalSourcePpdkConfig = new ExternalSourcePpdkConfig(true);
遥测配置:
public class TelemetryConfig {
static Gson gson = new Gson();
@JsonProperty(value="location", required = true)
@Valid
@NotNull(message="Please provide a valid location")
Location location;
@Valid
@JsonProperty(value="isEnabled", required = true)
@NotNull(message="Please provide a valid isEnabled")
Boolean isEnabled;
地点:
static public enum Location {
US("usa"),
EU("europe"),
CANADA("canada"),
ASIA("asia");
private String name;
private Location(String s) {
this.name = s;
}
private String getName() {
return this.name;
}
}
当我尝试发送以下 JSON 时:
{
"txConfig": {
"location": "asdsad"
}
}
API 返回空响应 400 错误请求,而我希望它验证位置是类的 ENUM 之一。我也希望它能够验证 isEnable 参数,而它没有,尽管我向它添加了所有可能的注释..
有什么想法吗?
【问题讨论】:
标签: java spring-boot jackson