Rest API中的验证不起作用
我无法使用验证注释。当传递注释在验证中应该捕获的值时,我没有得到为它们配置的消息 控制器:Rest API中的验证不起作用,rest,spring-data,Rest,Spring Data,我无法使用验证注释。当传递注释在验证中应该捕获的值时,我没有得到为它们配置的消息 控制器: @RestController 公共类AccountConfigController{ @自动连线 AccountConfigService AccountConfigService; @RequestMapping(value=“/${project.version}/acctconfigs”,method=RequestMethod.GET,products=MediaType.APPLICATION
@RestController
公共类AccountConfigController{
@自动连线
AccountConfigService AccountConfigService;
@RequestMapping(value=“/${project.version}/acctconfigs”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
公共列表viewAccountConfig()引发BankServiceException{
List accountConfigDTOList=accountConfigService.viewAccountConfig();
返回accountConfigDTOList;
}
@RequestMapping(value=“/${project.version}/acctconfigs”,method=RequestMethod.PUT,consumes=MediaType.APPLICATION\u JSON\u value)
公共布尔editAccountConfig(@RequestBody@Valid AccountConfigEditDTO AccountConfigEditDTO)引发BankServiceException{
返回accountConfigService.editAccountConfig(accountConfigEditDTO);
}
}
有两个DTO的使用,因为我没有得到的方式做需要使用一个
AccountConfigeditTo:
公共类AccountConfigEditDTO实现java.io.Serializable{
@JsonProperty(“accountconfig”)
@有效的
私有列表accountConfigViewDTOList;
@NotBlank(message=“accountconfig.lastUpdateId.mandatory”)
@大小(max=10,message=“accountconfig.lastUpdateId.Size”)
@JsonProperty(“用户ID”)
私有字符串lstUpdtId;
//Getter、setter和构造函数
}
AccountConfigViewDTO:
公共类AccountConfigViewDTO实现可序列化{
@NotNull(message=“newcustomer.acttype.mandatory”)
私有字符类型;
@NotEmpty(message=“accountconfig.interest.mandatory”)
@数字(整数=8,分数=4,message=“accountconfig.interest.size”)
私人银行;
@NotEmpty(message=“accountconfig.interest.mandatory”)
@数字(整数=8,分数=4,message=“accountconfig.minimumBalance.size”)
私人大资产负债表;
//getter、setter和构造函数
}
当我传递数据时,例如:
{
"accountconfig" : [
{
"acctType": "C",
"interestRate": "",
"minBalance": 500
},
{
"acctType":"S",
"interestRate": 4.00,
"minBalance": 5000.0
}
],
"userId": "admin"
}
我明白了
{“errorCode”:500,“errorMsg”:“发生未知错误。”}
但不是我的自定义信息“利率是强制性的”。我在application.properties文件中给出了此错误消息映射
请告诉我代码中是否有任何问题?最终尝试了各种选项,我找到了解决问题的方法。所以我想把它贴出来,以防其他人遇到这个问题 解决方案是在列表字段的AccountConfigEditDTO中添加@Valid:
public class AccountConfigEditDTO implements java.io.Serializable{
@JsonProperty("accountconfig")
private @Valid List<AccountConfigViewDTO> accountConfigViewDTOList;
@NotBlank(message="accountconfig.lastUpdatedId.mandatory")
@Size(max=10,message="accountconfig.lastUpdatedId.size")
@JsonProperty("userId")
private String lstUpdtId;
}
公共类AccountConfigEditDTO实现java.io.Serializable{
@JsonProperty(“accountconfig”)
private@Valid List accountConfigViewDTOList;
@NotBlank(message=“accountconfig.lastUpdateId.mandatory”)
@大小(max=10,message=“accountconfig.lastUpdateId.Size”)
@JsonProperty(“用户ID”)
私有字符串lstUpdtId;
}
这就成功了。但是验证char字段仍然没有成功,我认为唯一的方法是定制验证器注释
对于上面的问题,我理解的另一件事是,验证没有进入列表中的DTO,因此添加@Valid就完成了这项任务。如果有人对此有更好的解释,请发表评论。您是否在standard out上看到任何异常/堆栈跟踪?在示例数据中,您的第一个
acctType
的值是多少?输入错误或JSON无效?否。它应生成NOTNULL验证消息。因为该值不存在。控制台中也没有错误或异常,因为您的数据不是有效的JSON。“acctType”:null是什么意思?是的,这意味着accType为null,但由于字符验证存在大量问题,请立即查看更新的测试。BigDecimal字段应该给出错误消息,但它没有给出我的,也许它可以帮助。注意豆子。