我有一个Spring–JSON / RestAPI,它使用注释驱动的输入验证. @有效
当我尝试验证另一个对象内的对象时,我收到以下错误.
java.lang.IllegalStateException: JSR-303 validated property 'client.application' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:153) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:108) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.validation.DataBinder.validate(DataBinder.java:866) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
...
以下是此处使用的json数据模型:
{ // @Valid MessageDTO
"title": "Test","message":"Test","client": { // @Valid ClientDTO
"application": "Test"
}
}
我在这里跳过了java定义,因为它会有太多无用的噪音IMO.
我不想/不能轻易地将getter或setter添加到我的DTO中,那么我该如何关注该错误消息并将我的DataBinder配置为使用“直接字段访问”?我想使用JavaConfig(@Configuration).
使用以下依赖项:
编辑:
控制器:
@RequestMapping(path = "/send",method = { RequestMethod.POST,RequestMethod.PUT })
public void send(@Valid @RequestBody MessageDTO message)
MessageDTO:
public class MessageDTO {
...
@Valid
@NotNull
public ClientDTO client;
}
ClientDTO:
public class ClientDTO {
...
@NotEmpty
public String application;
}
最佳答案
原文链接:https://www.f2er.com/spring/431922.html