springMVC繫結引數時報錯DefaultHandlerExceptionResolver解決辦法
阿新 • • 發佈:2019-01-12
public class User { int id; String username; String password; String realName; String email; String phone; Set<Role> roles;}
UserAction.java
<span style="white-space:pre" > </span>@RequestMapping(value = "list") @ResponseBody public List list(@RequestParam Map map) { List<User> list = userService.list(map); return list; }
<span style="white-space:pre"> </span>@RequestMapping(value = "update")<span style="white-space:pre" > </span>@ResponseBody<span style="white-space:pre"> </span>public User update(User user) {<span style="white-space:pre"> </span>userService.update(user);<span style="white-space:pre"> </span>return user;<span style="white-space:pre" > </span>}
UserService.java
<span style="white-space:pre"> </span>public List<User> list(Map map) { List<User> list = userDAO.list(map); for (User user : list) { user.setRoles(null); } return list; }
因為User類中包含Set成員,由於Hibernate的延遲載入,所以在業務層吧roles置為null了。
所以list方法的返回結果如下:
[{"id":1,"username":"admin","password":"admin","realName":"張三","email":"[email protected]","phone":"23423234","roles":null},{"id":2,"username":"erwt","password":"admin","realName":"張三","email":"[email protected]","phone":"3423432432","roles":null}]
前臺用easyui的datagrid控制元件顯示和編輯資料:
<table id="dg" title="使用者列表" class="easyui-datagrid" style="width:100%;height:auto;margin:0 auto;" data-options="" idField="id" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr > <th field="id" width="60" data-options="align:'center', halign: 'center'">id</th> <th field="username" width="100" editor="text" data-options="align:'left', halign: 'center'">使用者名稱</th> <!-- 如果field 值easyui datagrid找不到的話,它會使用formatter的規則來繫結資料; --> <th field="password" width="100" editor="text" hidden="true">登入密碼</th> <th field="realName" width="100" editor="text" align="center">真實姓名</th> <th field="email" width="160" align="center" editor="text">郵箱</th> <th field="phone" data-options="align:'left', halign: 'center'" width="100" editor="text" >聯絡電話</th> </tr> </thead> </table>
這樣編輯easyui的表格資料後儲存,報異常
WARN DefaultHandlerExceptionResolver:189 - Handler execution resulted in exception: org.springframework.validation.BeanPropertyBindingResult: 1 errorsField error in object 'user' on field 'roles': rejected value []; codes [typeMismatch.user.roles,typeMismatch.roles,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.roles,roles]; arguments []; default message [roles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'roles'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.pelin.webapp.ms.entity.Role] for property 'roles[0]': no matching editors or conversion strategy found]
解決方案:
之前一直以為是springMVC的引數繫結功能和easyui的表格資料編輯功能不相容,於是就去看原始碼,希望找到根本原因,弄的頭都大了,還是找到原因。後來把roles置null改為roles.clear().居然能正常更新編輯了。
UserService.java
public List<User> list(Map map) { List<User> list = userDAO.list(map); for (User user : list) { user.getRoles().clear(); } return list; }
修改後list返回結果:
[{"id":1,"username":"admin","password":"admin","realName":"張三","email":"[email protected]","phone":"23423234","roles":[]},
{"id":2,"username":"erwt","password":"admin","realName":"張三","email":"[email protected]","phone":"3423432432","roles":[]}]
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://blog.csdn.net/jiangjunshow