lon*_*ngi 8 android json jackson retrofit
我使用的是Jackson 2.9.2和Retrofit 2.1.0一些POST与操作JSONArray作为HTML-Header参数。
API定义的值为aId。不管我怎么努力,我JSON property是始终转换为小写(aid)。
我使用测试了相同的代码abId,并且可以正常工作...任何提示,我的配置错误或针对该属性名称的约定(?)?
//ObjectMapper initialization
ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
//the data class
import com.fasterxml.jackson.annotation.JsonProperty
data class MyClass(
@JsonProperty
val aId: String? = null, //<-- not working
@JsonProperty
val abId: String? = null //working!
)
//Retrofit call
import retrofit2.http.Body
@POST("log")
fun sendLog(@Body logs: List<MyClass>): Call<MyCall>
//JSON Result in HTML Header
[{
"aid":"some_value", //should be "aId"
"abId":"some_value" //is correct
}]
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下注释:
@SerializedName(“ aId”)
@JsonProperty(“ aId”)
@JsonRawValue
@JsonAlias
请参阅 Michael Ziober 发布的链接以获取答案Usage of Jackson @JsonProperty 注释用于 kotlin 数据类
所描述的问题是由于Jackson's默认行为不扫描私有字段而导致的。这种行为可以改变@JsonAutoDetect
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
data class MyClass(
@JsonProperty
val aId: String? = null,
@JsonProperty
val abId: String? = null
)
Run Code Online (Sandbox Code Playgroud)