当我试图通过控制器传输大量数据时,我得到一个错误。
有一个意外的错误(type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'com.psu.projectmethod.domain.wrappers.UserSet'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.ModelAttribute com.psu.projectmethod.domain.wrappers.UserSet] for value 'com.psu.projectmethod.domain.User@23'; nested exception is java.lang.IllegalArgumentException: 无法实例化Collection类型:com.psu.projectmethod.domain.wrappers.UserSet org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.psu.projectmethod.domain.wrappers.UserSet'; 嵌套异常是 org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.ModelAttribute com.psu.projectmethod.domain.wrappers.UserSet] for value 'com.psu.projectmethod.domain.User@23'; nested exception is java.lang.IllegalArgumentException: 不能实例化集合类型:com.psu.projectmethod.domain.wrappers.UserSet。
我根据stackoverflow的例子和这篇文章做了所有的事情。https:/www.viralpatel.netspring-mvc-multi-row-submit-java-list. 请帮我解决这个问题((
我的控制器方法。
@PreAuthorize("hasAuthority('_2_TEACHER')")
@GetMapping("/project/{projectId}/party/create")
public String viewTeacherCreateProjectParty(
@PathVariable("projectId") Project project,
Party party,
Model model) {
Set<User> users = userService.userList(Sort.by("username"));
UserSet userSet = new UserSet(users);
model.addAttribute("project", project);
model.addAttribute("party", party);
model.addAttribute("users", userSet.getUsers());
return "teacherProjectPartyCreate";
}
@PreAuthorize("hasAuthority('_2_TEACHER')")
@PostMapping("/project/{projectId}/party/create")
public String processCreateProjectParty(
@PathVariable("projectId") Project project,
@Valid Party party,
@ModelAttribute("users") UserSet users,
Model model) {
model.addAttribute("project", project);
model.addAttribute("party", party);
Long projectId = projectService.createProjectParty(project, party, users);
return "redirect:/projects/project/" + projectId;
}
我的控制器方法: Freemarker形式。
<form action="/projects/project/${project.projectId}/party/create" method="post"
style="color: #757575;">
<!-- CSRF Token -->
<input type="hidden" name="_csrf" value="${_csrf.token}"/>
<div class="md-form">
<input type="text" id="partyName" name="partyName" value="${party.partyName!''}"
class="form-control ${(partyNameError??)?string('is-invalid', '')}" required>
<label for="partyName">Group name</label>
<#if partyNameError??>
<div class="invalid-feedback">
${partyNameError}
</div>
</#if>
</div>
<select name="users" class="selectpicker"
data-header="Select users"
data-live-search="true"
data-selected-text-format="count"
data-size="auto"
data-style="btn-unique"
data-width="auto"
multiple
title="Select users"
>
<#list users as user>
<option value="${user}">${user.fullname}</option>
</#list>
</select>
<div class="modal-footer d-flex justify-content-center">
<button type="button" onClick='location.href="/projects/project/${project.projectId}"'
class="btn btn-outline-info waves-effect">
Отмена
</button>
<button type="submit" class="btn btn-primary waves-effect">
Save
</button>
</div>
</form>
我的实体创建方法
public Long createProjectParty(Project project, Party party, Set<User> users) {
project.addParty(party);
party.addUsers(party, users);
projectRepo.save(project);
return project.getProjectId();
}
添加双向关联的辅助方法。
public void addParty(Party party) {
party.setPartyProject(this);
this.projectParties.add(party);
}
public void addUsers(Party party, Set<User> users) {
party.setPartyUsers(users);
this.partyUsers.addAll(users);
}
ManyToMany关联映射。
@ManyToMany(fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE}
)
@JoinTable(name = "party_users",
joinColumns = {@JoinColumn(name = "fk_party_id")},
inverseJoinColumns = {@JoinColumn(name = "fk_user_id")})
private Set<User> partyUsers = new HashSet<>();
public class UserSet implements Set<User> {
private Set<User> users;
public UserSet(Set<User> users) {
this.users = users;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
... // getters, setters and Set metods
在分析了你的日志后,我发现是你的请求出了问题。
仔细阅读这个日志条目。
Failed to convert value of type 'java.lang.String' to required type 'com.psu.projectmethod.domain.wrappers.UserSet'
看来你是想发送 String
在该地 UserSet
. 如果需要这种行为,请考虑使用转换器。检查 文章
我没有看到你所有的类的定义,但是在事情上似乎是一个任意的和一个奇怪的一起。
看com.psu.projectmethod.domain.wrappers.UserSet]的值'com.psu.projectmethod.domain.User@23';嵌套异常是java.lang.IllegalArgumentException。无法实例化集合类型:com.psu.projectmethod.domain.wrappers.UserSet 1。为什么Spring一定要能从一个String转换到你的自定义类型?2. 参见 "对于值'com.psu.projectmethod.domain.User@23';嵌套异常是java.lang.IllegalArgumentException。Could not instantiate Collection type: com.psu.projectmethod.domain.wrappers.UserSet "为什么在处理User@23这个实例时,它试图实例化UserSet?ModelAndView是如何知道你的UserSet类型的,为什么它提到了它?你有一个循环引用吗?请仔细检查你传输给Model的是什么,以及在转换中发生了什么。