Java – Failed to convert type string to type long Spring Boot

h2javajspspringspring-boot

I am trying to add a value of type double to my database using a form. However, when I try doing this I get an error stating:

Failed to convert value of type [java.lang.String] to required type
[com.javainuse.model.Grade]; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type [java.lang.Long] for
value '2.1'; nested exception is java.lang.NumberFormatException: For
input string: "2.1"

If I add a whole number, however, the entry is added with no problem. Can anyone pinpoint where my error is? I've looked at similar questions but nothing seems to work 🙁 My code is as follows:

Grade Controller:

@Controller
public class GradeController {

    @Autowired
    private GradeRepository gradeData;

    @RequestMapping(value = "/addNewGrade.html", method = RequestMethod.POST)
    public String newGrade(Grade grade) {

        gradeData.save(grade);
        return ("redirect:/listModules.html");

    }

    @RequestMapping(value = "/addNewGrade.html", method = RequestMethod.GET)
    public ModelAndView addNewGrade() {

        Grade grd = new Grade();
        return new ModelAndView("newGrade", "form", grd);

    }

Grade repository:

package com.javainuse.data;

import org.springframework.data.jpa.repository.JpaRepository;

import com.javainuse.model.Grade;

public interface GradeRepository extends JpaRepository<Grade, Long> {

}

Add New Grade form:

  <form name="form" action="" method="POST">
        <div>
            <label>Grade </label>
            <input name="grade" />
        </div>     
        <div>
            <label>Module</label>
            <select name="moduleid">
                <c:forEach items="${module.rows}" var="row">
                    <option value="${row.id}">${row.modulename}</option>
                </c:forEach>
            </select>
        </div>    
        <div>
            <label>Student</label>
            <select name="studentid">
                <c:forEach items="${student.rows}" var="row">
                    <option value="${row.id}">${row.firstname} ${row.lastname}</option>
                </c:forEach>
            </select>
        </div>    
        <br>
        <input type="submit" value="Submit"/>
    </form>

Grade Model:

@Entity
public class Grade {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private long id;
    private int moduleid;
        private int studentid;
        private double grade;

    public int getModuleid() {
        return moduleid;
    }

    public void setModuleid(int moduleid) {
        this.moduleid = moduleid;
    }

    public int getStudentid() {
        return studentid;
    }

    public void setStudentid(int studentid) {
        this.studentid = studentid;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

}

The datatype in my database is also double.

Best Answer

It tries to bind grade passed inside form data to controller method parameter:

You can either:

  • add @ModelAttribute annotation to your controller method like:
@RequestMapping(value = "/addNewGrade.html", method = RequestMethod.POST)
public String newGrade(@ModelAttribute("form") Grade grade) {

    gradeData.save(grade);
    return "redirect:/listModules.html";

}
  • change name of grade field in Grade class into something different (for example value).