@JsonIgnore在Scala案例类中不起作用

HoT*_*icE 6 json scala

我有一个简单的案例课

case class Project(@JsonIgnore id: Option[UUID], name: Option[String])
Run Code Online (Sandbox Code Playgroud)

我正在使用com.fasterxml.jackson

com.fasterxml.jackson.core:jackson-databind:2.8.4
com.fasterxml.jackson.core:jackson-annotations:2.8.4
org.skinny-framework.com.fasterxml.jackson.module:jackson-module-scala_2.12:2.8.4
Run Code Online (Sandbox Code Playgroud)

...

private val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.writeValueAsString(project) 
Run Code Online (Sandbox Code Playgroud)

尽管@JsonIgnore将ID写入结果json

我究竟做错了什么?

更新:

当前的解决方法:

@JsonIgnoreProperties(Array("id"))
case class Project(id: Option[UUID], name: Option[String]) 
Run Code Online (Sandbox Code Playgroud)

这很好用:)

小智 0

注释只是以构造函数参数而不是字段结束。

来自 scala文档

By default, annotations on (val-, var- or plain) constructor parameters end up on the parameter, not on any other entity
Run Code Online (Sandbox Code Playgroud)

您需要使用 scala 的元注释将注释放在字段上。尝试这个

By default, annotations on (val-, var- or plain) constructor parameters end up on the parameter, not on any other entity
Run Code Online (Sandbox Code Playgroud)