java.lang.NoSuchMethodException:com ... Employee。<init>()

时间:2017-10-22 00:30:32

标签: spring spring-boot kotlin

我有这个类,我有一个构造函数:

@Entity
data class Employee(
    @field: Id
    @field:GeneratedValue var id : Long = 0,
    var username : String = "",
    var password : String ="",
    var name : String ="",
    var lastName: String ="",
    var phone : String="",
    var email : String ="",
    var sex : String ="",
    var active : Boolean = false,
    @field: ManyToOne(targetEntity = District::class)
    var district : District?,
    @field: ManyToOne(targetEntity = Company::class)
    var company : Company?,
    var picture:String="",
    var resetPasswordCode:String="",
    // código que se le envía al usuario en el momento que crea
    // la cuenta, deberá abrir en enlace del correo para activarla
    // de ahí que "active" sea false.
    var activationCode : String="",
    var enabled : Boolean = true,
    var accountNonExpired: Boolean = true,
    var credentialsNonExpired: Boolean = true,
    var accountNonLocked : Boolean = true,
    @field: OneToMany(targetEntity = Roles::class) var roles :MutableSet<Roles> = mutableSetOf())

{
    fun toUser() : User
    {
        val authorities = mutableSetOf<GrantedAuthority>()
        roles.forEach{authorities.add(SimpleGrantedAuthority(it.role))}
        return User(username,password,enabled,accountNonExpired,credentialsNonExpired,accountNonLocked, authorities)
    }
}

我有一个控制器方法并且用于测试,我只是打印“保存”文本(因为我遇到了这个问题,对我来说没有任何意义):

@RequestMapping(method = arrayOf(RequestMethod.POST))
fun doPost(employee: Employee) : String {
    println("saving!!!")
    // employeeService.save(employee)
    return "redirect:/display"
}

发送表单时,我收到此错误:

java.lang.NoSuchMethodException: com.almasoft.facturapp.entities.Employee.<init>()

表格是这样的:

<form th:action="@{/register}" method="post" role="form" 
    th:object="${employee}">
    <legend>Register a User</legend>

    <div class="form-group">
        <label for="username">User Name</label>
        <input type="text" class="form-control" name="username" id="username" th:field="*{username}"></input>
    </div>

    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" class="form-control" name="password" id="password" th:field="*{password}"></input>
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

我正在关注this帖子并根据我的需求调整代码,但我无法将其付诸实践:(

那么,如果有一个默认构造函数,为什么会抱怨默认构造函数?

并且:我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

@field: ManyToOne(targetEntity = District::class)
var district : District?,
@field: ManyToOne(targetEntity = Company::class)
var company : Company?,

这些没有默认值,因此不会生成空构造函数。

为每个人添加= null