如何修复错误:无法将类型'java.lang.String'的值转换为所需类型'java.lang. Long';

vohkndzv  于 2022-12-10  发布在  Java
关注(0)|答案(2)|浏览(520)

我正在开发一个项目管理器-Web应用程序,它允许您以成员身份登录并加入不同类型的项目。成员可以参与多个项目。项目和成员实体通过成员实体连接。每次登录的成员加入一个项目时,都会创建一个新的成员实体。
然而,我有麻烦创建一个新的成员后,成员点击Join-button joinProject.html -page.我一直收到以下错误消息:

(type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "saveMembership"

目标是将登录的成员添加到项目中。新成员可以在projectDetails.html页面上看到。
我用的是Sping Boot ,Thymeleaf和JPA。我感觉这是一个非常简单和愚蠢的东西,但我就是找不到问题所在。
如果你需要看到更多的代码或信息我将很乐意提供
感谢您抽出宝贵时间!
下面是我尝试使用的MembershipCreatorObject类,它将所需的数据放入我的控制器:

public class MembershipCreatorObject {

    private String id;
    private String username;
    private String jobDescription;

    public MembershipCreatorObject() {
        super();
    }

    public MembershipCreatorObject(String id, String username, String jobDescription) {
        super();
        this.id = id;
        this.username = username;
        this.jobDescription = jobDescription;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getJobDescription() {
        return jobDescription;
    }

    public void setJobDescription(String jobDescription) {
        this.jobDescription = jobDescription;
    }

下面是我的控制器:

@RequestMapping(value= {"/joinProject/{projectId}"})
    public String joinProject(@PathVariable("projectId") Long urlId, Model model) {

        model.addAttribute("mObject", new MembershipCreatorObject());

        model.addAttribute("project", projectRepo.findByProjectId(urlId));

        return "joinProject";
    }

    @RequestMapping(value="/saveMembership", method=RequestMethod.POST)
    public String saveMembership(MembershipCreatorObject mObject) {

        String id = mObject.getId();

        Long idConverted = Long.parseLong(id);

        String username = mObject.getUsername();

        String description = mObject.getJobDescription();

        Project projectToAdd = projectRepo.findByProjectId(idConverted);

        Member memberToAdd = memberRepo.findByUsername(username);

        Membership membershipToAdd = new Membership(description, memberToAdd, projectToAdd);

        membershipRepo.save(membershipToAdd);

        return "redirect:/projectCatalog";
    }

下面是joinProject-html文件。

<h1>Join Project</h1>

    <h4 th:inline="text">Logged in as: [[${#httpServletRequest.remoteUser}]]</h4>

    <form th:object="${mObject}" th:action="@{saveMembership}" action="#" style="padding: 20px;" method="POST">

        <input th:field="*{id}" />
        <!-- th:value="${project.projectId}" -->

        <input type="text" th:field="*{username}" />
        <!-- th:value="${#httpServletRequest.remoteUser}" -->

        <label>What will you be working on?</label>
        <input type="text" th:field="*{jobDescription}" style="display: block;" />

        <input type="submit" value="Join" style="display: block; margin-top:10px; width:70px;">

    </form>
j91ykkif

j91ykkif1#

看起来您正在尝试将文字字符串“saveMembership”转换为long,而不是long的字符串表示形式,例如:

String str = "12323445664546544564974";
Long lng = Long.parseLong(str);

这是我基于异常堆栈跟踪的假设,异常堆栈跟踪声明“NumberFormatException:对于输入字符串:“保存成员身份””

wixjitnu

wixjitnu2#

我不是html或thymeleafMaven,但我认为您在html中输入了错误的内容。我认为当您在submit上单击时,您的代码会向/joinProject/saveMembership而不是/saveMembership发送一个POST请求。您可以在浏览器中轻松地进行测试(检查POST请求的URL是什么)

相关问题