无法将类型“ java.lang.String”的值转换为所需的类型“ int”;

时间:2019-06-23 04:59:57

标签: spring-boot spring-mvc jsp jpa jstl

将id值传递给控制器​​会产生错误“无法将类型'java.lang.String'的值转换为所需的类型'int'”。

但是当我打印System.out.println(studentService.findById(id))时,它在终端中给出输出为 可选[Student {id = 1,firstName ='Abhishek',lastName ='Shukla',branch ='CS',year = 4,mobileNumber ='9876543210'}]

我已经尝试过将id作为字符串传递,并通过在控制器中使用Integr.parseInt(id)将字符串转换为int,但它会引发错误  “路径为[]的上下文中的servlet [dispatcherServlet]的Servlet.service()引发了异常[请求处理失败;嵌套异常为java.lang.NumberFormatException:对于输入字符串:“ {id}”],根本原因为“


    <h1>Student Place</h1>
    <form name="myForm" action="/student/{id}" >
        <table border="1">
            <tbody>
                <tr>
                    <td>Student ID :</td>
                    <td><input type="text" name="id" size="10" /> </td>
                </tr>
            </tbody>
         </table>
         <br>
        <input type="submit" value="submit" name="submit" /><br>
        <input type="reset" value="Rest" name="reset" />
     </form>


@Controller
public class StudentController {

    @Autowired
    private StudentService studentService  ;

    @GetMapping(value = "/students")
    public  String index(ModelMap modelMap){
        modelMap.put("students" , studentService.findAll());
        return "index" ;
    }

    @GetMapping(value = "/student/{id}")
    public String data(@PathVariable("id") int id , ModelMap modelMap){
        modelMap.put("students" , studentService.findById(id)) ;
        System.out.println(studentService.findById(id));

        return "studentbyid" ;
    }

    @GetMapping(value = "/student")
    public String databyid(){
        return "student" ;
    }
}



public interface StudentService {

    public Iterable<Student> findAll() ;
    public Optional<Student> findById(int id) ;
}


@Transactional
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository ;

    @Override
    public Iterable<Student> findAll() {
        return studentRepository.findAll();
    }

    public Optional<Student> findById(int id) {
        return studentRepository.findById(id);
    }
}


@Entity
@Table(name = "student")
public class Student {

    @Id
    @Column(name="id")
    private int id ;

    @Column(name="first_name")
    private String firstName ;

    @Column(name="last_name")
    private String lastName ;

    @Column(name="branch")
    private String branch ;

    @Column(name="year")
    private int  year ;

    @Column(name="mobile_number")
    private String mobileNumber ;

    public Student() {
    }

    public Student(int id, String firstName, String lastName, String branch, int year, String mobileNumber) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.branch = branch;
        this.year = year;
        this.mobileNumber = mobileNumber;
    }

    public int getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", branch='" + branch + '\'' +
                ", year=" + year +
                ", mobileNumber='" + mobileNumber + '\'' +
                '}';
    }
}

Whitelabel错误页面 此应用程序没有针对/ error的显式映射,因此您将其视为后备。

发生意外错误(类型=错误请求,状态= 400)。 无法将类型“ java.lang.String”的值转换为所需的类型“ int”;嵌套的异常是java.lang.NumberFormatException:对于输入字符串:“ {id}”

2 个答案:

答案 0 :(得分:0)

您表单的操作是

action="/student/{id}"

因此,如错误消息所示,您实际上是在向/student/{id}发送请求,而不是/student/1234

显然,由于表单具有一个输入字段,用户必须在其中输入ID,因此ID将作为请求参数(而不是路径变量)发送,因此,控制器方法和操作的路径应为

/students

,并且应将ID作为请求参数:

@GetMapping(value = "/student")
public String data(@RequestParam("id") int id, ModelMap modelMap) 

答案 1 :(得分:0)

请检查属性文件并确保在指定属性后没有输入分号(;)