当我试图通过控制器传输大量数据时,我得到一个错误。

有一个意外的错误(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. 如果需要这种行为,请考虑使用转换器。检查 文章

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何从Flutter项目中建立APK文件?

2022-9-8 17:47:38

未分类

在R中更新data.table中的整行。

2022-9-8 17:47:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索