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

时间:2019-04-15 21:18:07

标签: java spring spring-boot spring-data-jpa thymeleaf

我正在开发一个项目经理-网络应用程序,该应用程序允许您以成员身份登录并加入各种项目。成员可以参与多个项目。项目和成员实体通过成员实体连接。每次登录的成员加入项目时,都会创建一个新的成员实体。

但是,在成员单击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 -page上看到新的成员资格。

我正在使用Spring 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>

2 个答案:

答案 0 :(得分:0)

您似乎正在尝试将文字字符串“ saveMembership”转换为long,而不是将long的字符串表示形式转换为:

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

这是我基于异常stacktrace的假设,该异常声明为“ NumberFormatException:对于输入字符串:“ saveMembership””

答案 1 :(得分:0)

我不是html或百里香专家,但我认为您在html中输入了错误的内容。 我相信当您在POST上碰碰时,您的代码会向/joinProject/saveMembership而不是/saveMembership发送一个[1554334020000, 1554334200000, 1554334380000, 1554334560000, 1554334740000]请求。您可以在浏览器中轻松进行测试(检查POST请求的网址是什么)