我试图获取使用bootstrap数据表的用户列表,但我一直得到StackOverflowError
。以下是我的代码。
控制器:
@RequestMapping(value = "/admin/login_management", method = RequestMethod.GET)
public ModelAndView loginMgt(HttpServletRequest request, ModelMap m) {
m.put("login_management", true);
return new ModelAndView("login_management");
}
(数据表对象)
@RequestMapping(value = "/admin/usersList", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody Object userListPaginationDataTables(@RequestParam(value = "sEcho", required = false) String sEcho,
@RequestParam(value = "sSearch", required = false) String sSearch,
@RequestParam(value = "sColumns", required = false) String sColumns,
@RequestParam(value = "iDisplayStart", required = false) Integer iDisplayStart,
@RequestParam(value = "iDisplayLength", required = false) Integer iDisplayLength,
@RequestParam(value = "iColumns", required = false) Integer iColumns,
@RequestParam(value = "iSortCol_0", required = false) Integer iSortCol_0,
@RequestParam(value = "iSortColumnIndex", required = false) Integer iSortColumnIndex,
@RequestParam(value = "sSortDir_0", required = false) String sSortDir_0)
throws IOException {
String json = "";
List<User> result = new ArrayList<>();
result = userService.getUserList(iDisplayStart, iDisplayLength);
int total = userService.getUserTotalSize();
json = getJSonString(result, total);
return json;
}
private static String getJSonString(List<?> result, int total){
String json="";
try {
DataTablesObject dataTablesObject = new DataTablesObject();
dataTablesObject.setAaData(result);
if(total > 0) {
dataTablesObject.setiTotalRecords(total);
dataTablesObject.setiTotalDisplayRecords(total);
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
json = gson.toJson(dataTablesObject);
} catch (Exception ex) {
LOGGER.fatal(ex);
}
return json;
}
login_management.jsp:
<script>
$(function() {
$("#example2").dataTable({
"bServerSide": true,
"sAjaxSource": "${pageContext.request.contextPath}/admin/usersList",
"bProcessing": true,
"bFilter":false,
"sPaginationType": "bootstrap",
"bJQueryUI": true,
"aoColumns": [
{ "mData": "id" },
{ "mData": "login" },
{
"mData": null,
"mRender": function (data, type, full) {
//alert(data);
return "<form method='post' action='${pageContext.request.contextPath}/admin/editUser?id=1'>"+
"<button type='submit'class='btn btn-primary'>Edit</button>"+
"</form>";
},
"fnCreatedCell": function ( cell ) {
$('input, label', cell)
.button( {
text: true
} );
}
}
]
});
});
</script>
型号:
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
private Integer id;
private String login;
private String password;
@OneToOne(cascade=CascadeType.ALL, fetch =FetchType.EAGER)
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
)
private Role role;
//...getters & setters...
}
角色:
@Entity
@Table(name="roles")
public class Role {
@Id
@GeneratedValue
private Integer id;
private String role;
@OneToMany(cascade=CascadeType.ALL, fetch =FetchType.EAGER)
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")}
)
private Set<User> userRoles;
//...getters and setters...
}
@Entity
@Table(name="user_roles")
public class UserRole {
@Id
@Column(name="user_id")
private Integer id;
@Column(name="role_id")
private Integer roleId;
//...getters and setters...
}
然而,在我编译并启动我的tomcat之后,我收到了这个错误:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path threw exception [Handler processing failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError
at com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:580)
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:401)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:495)
at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:246)
at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:231)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:894)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:894)
造成这种情况的原因是什么?