添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How do I fix error: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long';

Ask Question

I'm working on a project manager -web app that allows you to sign in as a member and join different kinds of projects. Members can take part in multiple projects. Project and Member -Entities are connected by a Membership -Entity. Every time a logged in member joins a project a new membership Entity is created.

However I'm having trouble creating a new membership after member clicks Join-button on joinProject.html -page. I keep getting following error message:

(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"

The goal is to add logged in member to a project. The new membership can be seen listed on projectDetails.html -page.

I'm using Spring Boot, Thymeleaf and JPA. I have a feeling this is something really simple and stupid but I just can't find the problem. :(

If you need to see more code or information I will be happy to provide.

Thank you for your time!

Here is the MembershipCreatorObject-class that I try to use to get the needed data into my controller:

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;

Here is my controller:

@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";

Here is the joinProject -html file.

    <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>

It looks like you're trying to convert the literal string "saveMembership" to a long, rather than the string representation of a long such as:

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

This is my assumption based on the exception stacktrace which states "NumberFormatException: For input string: "saveMembership""

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.