实体类No default constructor found 找不到默认构造函数;

Posted jnhs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实体类No default constructor found 找不到默认构造函数;相关的知识,希望对你有一定的参考价值。

root cause

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.hs.model.StudentModel]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.hs.model.StudentModel.<init>()
	org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:108)
	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:136)
	org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:101)
	org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
	org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

 

root cause

java.lang.NoSuchMethodException: com.hs.model.StudentModel.<init>()
	java.lang.Class.getConstructor0(Class.java:2902)
	java.lang.Class.getDeclaredConstructor(Class.java:2066)
	org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:136)
	org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:101)
	org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
	org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

实体类中已经有了构造函数了getter setter方法

技术分享图片
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hs.model;

 
 
/**
 *
 * @author C
 */
public class StudentModel {
    private int studentIdInModel;
    private String studentNameInModel;
    private int studentAgeInModel;

    public StudentModel(int studentIdInModel, String studentNameInModel, int studentAgeInModel) {
        this.studentIdInModel = studentIdInModel;
        this.studentNameInModel = studentNameInModel;
        this.studentAgeInModel = studentAgeInModel;
    }

    public int getStudentIdInModel() {
        return studentIdInModel;
    }

    public void setStudentIdInModel(int studentIdInModel) {
        this.studentIdInModel = studentIdInModel;
    }

    public String getStudentNameInModel() {
        return studentNameInModel;
    }

    public void setStudentNameInModel(String studentNameInModel) {
        this.studentNameInModel = studentNameInModel;
    }

    public int getStudentAgeInModel() {
        return studentAgeInModel;
    }

    public void setStudentAgeInModel(int studentAgeInModel) {
        this.studentAgeInModel = studentAgeInModel;
    }
    
 
     
}
实体类的代码

 



添加语句如下,就可以,意思是构造一个空的对象?

技术分享图片

 















以上是关于实体类No default constructor found 找不到默认构造函数;的主要内容,如果未能解决你的问题,请参考以下文章

Spring Jpa实体实例化失败:org.springframework.orm.jpa.JpaSystemException: No default constructor for entity

SpringBoot 提示:java.lang.IllegalStateException: No primary or default constructor found for interface

Java中子类继承父类关于构造方法的问题:there is no default constructor available in

No default constructor found; nested exception is java.lang.NoSuchMethodException: com.rong.tms.serv

Jackson反序列JSON为实体对象出现:no String-argument constructor/factory method to deserialize from String value

编译器什么情况下会为没有Constructor的类生成一个Default Constructor