"Invalid property 'ssn' of bean class [java.lang.String]" 尝试使用 Spring 验证请求正文时
"Invalid property 'ssn' of bean class [java.lang.String]" when trying to validate request body with Spring
我有一个 springboot 应用程序,上面有一个休息控制器。用户通过 /test
访问控制器并传入一个 json 像这样:
{"ssn":"123456789"}
我想通过至少确保没有像这样传入的空 ssn 来验证输入:
{"ssn":""}
这是我的控制器:
@RequestMapping(
value = "/test",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json")
@ResponseBody
public JsonNode getStuff(@RequestHeader HttpHeaders header,
@RequestBody String payload,
BindingResult bindingResult) {
validator.validate(payload, bindingResult);
if(bindingResult.hasErrors()) {
throw new InvalidRequestException("The request is incorrect", bindingResult);
}
/* doing other stuff */
}
这是我的验证器:
@Component
public class RequestValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return false;
}
@Override
public void validate(Object target, Errors errors) {
ObjectMapper mapper = new ObjectMapper();
JsonNode ssn = null;
try {
ssn = mapper.readTree((String) target);
} catch (IOException e) {
e.printStackTrace();
}
if(ssn.path("ssn").isMissingNode() || ssn.path("ssn").asText().isEmpty()) {
errors.rejectValue("ssn", "Missing ssn", new Object[]{"'ssn'"}, "Must provide a valid ssn.");
}
}
}
我试着用邮递员测试这个,但我一直收到这个错误:
HTTP Status 500 - Invalid property 'ssn' of bean class [java.lang.String]: Bean property 'ssn' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
这里到底有什么问题?我不明白它在谈论与 getter 和 setter 相关的内容。
编辑1:请求的payload值
{"ssn":""}
默认情况下 Spring 引导配置 Json 解析器,因此您传递给控制器的任何 Json 都将被解析。 Spring 需要一个名为 'ssn' 的 属性 对象来绑定请求值。
这意味着您应该像这样创建一个模型对象:
public class Data {
String ssn;
}
并使用它像这样绑定您的请求正文:
@RequestMapping(
value = "/test",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json")
@ResponseBody
public JsonNode getStuff(@RequestHeader HttpHeaders header,
@RequestBody Data payload,
BindingResult bindingResult) {
validator.validate(payload, bindingResult);
if(bindingResult.hasErrors()) {
throw new InvalidRequestException("The request is incorrect", bindingResult);
}
/* doing other stuff */
}
您还需要调整您的验证器以使用这个新的数据对象。
我有一个 springboot 应用程序,上面有一个休息控制器。用户通过 /test
访问控制器并传入一个 json 像这样:
{"ssn":"123456789"}
我想通过至少确保没有像这样传入的空 ssn 来验证输入:
{"ssn":""}
这是我的控制器:
@RequestMapping(
value = "/test",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json")
@ResponseBody
public JsonNode getStuff(@RequestHeader HttpHeaders header,
@RequestBody String payload,
BindingResult bindingResult) {
validator.validate(payload, bindingResult);
if(bindingResult.hasErrors()) {
throw new InvalidRequestException("The request is incorrect", bindingResult);
}
/* doing other stuff */
}
这是我的验证器:
@Component
public class RequestValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return false;
}
@Override
public void validate(Object target, Errors errors) {
ObjectMapper mapper = new ObjectMapper();
JsonNode ssn = null;
try {
ssn = mapper.readTree((String) target);
} catch (IOException e) {
e.printStackTrace();
}
if(ssn.path("ssn").isMissingNode() || ssn.path("ssn").asText().isEmpty()) {
errors.rejectValue("ssn", "Missing ssn", new Object[]{"'ssn'"}, "Must provide a valid ssn.");
}
}
}
我试着用邮递员测试这个,但我一直收到这个错误:
HTTP Status 500 - Invalid property 'ssn' of bean class [java.lang.String]: Bean property 'ssn' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
这里到底有什么问题?我不明白它在谈论与 getter 和 setter 相关的内容。
编辑1:请求的payload值
{"ssn":""}
默认情况下 Spring 引导配置 Json 解析器,因此您传递给控制器的任何 Json 都将被解析。 Spring 需要一个名为 'ssn' 的 属性 对象来绑定请求值。
这意味着您应该像这样创建一个模型对象:
public class Data {
String ssn;
}
并使用它像这样绑定您的请求正文:
@RequestMapping(
value = "/test",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json")
@ResponseBody
public JsonNode getStuff(@RequestHeader HttpHeaders header,
@RequestBody Data payload,
BindingResult bindingResult) {
validator.validate(payload, bindingResult);
if(bindingResult.hasErrors()) {
throw new InvalidRequestException("The request is incorrect", bindingResult);
}
/* doing other stuff */
}
您还需要调整您的验证器以使用这个新的数据对象。