ZhangZuoliang
ZhangZuoliang

Reputation: 35

use kotlin for springmvc Could not instantiate bean class dataclass

i'm use the kotlin to ceate a simple ArticlesService,i create a Articles dataclass

data class Articles(var artid: Int, var artTitle: String, var artContent: String, var artAut: String, var artTime: Date)

but the class Could not instantiate bean class:No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zxl.blog.server.Articles.()

@Controller
class mainServer() {
@Autowired val artSer: ArticlesService? = null
@RequestMapping("/i")
fun fuwuqi(name: String, model: ModelMap): String {
    model.put("name", name)
    return "i"
}

@RequestMapping(value = "/saveArt", method = arrayOf(RequestMethod.POST))
fun saveArt(art: Articles): String {
    return if (artSer!!.save(art)) "saved" else "savefail";
}

@RequestMapping("/arts")
fun arts(): String {
    return "articles";
}
}

i think maybe the spring mvc not support the kotlin dataclass auto inject right?

Upvotes: 2

Views: 2236

Answers (1)

JB Nizet
JB Nizet

Reputation: 692131

From the documentation:

On the JVM, if the generated class needs to have a parameterless constructor, default values for all properties have to be specified (see Constructors).

data class User(val name: String = "", val age: Int = 0)

Upvotes: 5

Related Questions