相反,禁用MapperFeature.AUTO_DETECT_GETTERS
并@JsonGetter
在字段上使用。
这对我有用:
import com.fasterxml.jackson.annotation.JsonGetter
import com.fasterxml.jackson.databind.{MapperFeature, ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
mapper.disable(MapperFeature.AUTO_DETECT_GETTERS)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
class ScalaClass {
val field = "someField"
@JsonGetter val includedField = "fieldValue"
}
mapper.writer().writeValueAsString(new ScalaClass) // => res: String = {"includedField":"fieldValue"}
Java 类比您的 Scala 类更有可能是:
public class JavaClass {
public String getField() { return "someField"; }
}
UPDATE:用于@JsonGetter
在序列化中包含该字段。