無法顯示自己定義的錯誤信息(金額必填)
我在Girl實體類里定義了@NotNull,但是最后顯示在客戶端不是我在message中填的而是一段錯誤代碼
{ ??"code":?1, ??"msg":?"Failed?to?convert?property?value?of?type?'java.lang.String'?to?required?type?'double'?for?property?'money';?nested?exception?is?java.lang.NumberFormatException:?empty?String", ??"data":?null }
我的Girl對應出的代碼如下:
@Id @GeneratedValue private?Integer?id; @NotBlank(message?=?"這個字段必須傳") private?String?cupSize; @Min(value?=?18,?message?=?"未成年少女禁止入內") private?Integer?age; @NotNull(message?=?"金額必穿") private?Double?money;
Controller處對應代碼:
@PostMapping("/girls") ????public?Result<Girl>?girlAdd(@Valid?Girl?girl,?BindingResult?bindingResult){ ????????????Result?result?=?new?Result(); ????????if?(bindingResult.hasErrors()){ ????????????result.setCode(1); ????????????result.setMsg(bindingResult.getFieldError().getDefaultMessage()); ????????????return?result; ????????} //????????girl.setCupSize(girl.getCupSize()); //????????girl.setAge(girl.getAge()); ????????//上述兩行實際上是不用加的??傳值時就已經設值了,在Girl實體類里有Getter,Setter方法 ????????result.setCode(0); ????????result.setMsg("成功"); ????????result.setData(girlRepository.save(girl)); ????????return?result; ????}
我試過,如果cupSize不填,他正常報錯,但是金額不填就不是這么回事了。百思不得其解
2017-10-29
在Controller打個斷點看一下money是什么數據,還有就是NotBlank 與NotEmpty ?NotNull 進行區別 ? 你先打個斷點看一下money是null 還是“”空串