8

I'm using Jackson 2.9.2 and Retrofit 2.1.0 for some POST operation with a JSONArray as HTML-Header parameter.

The API defines a value which is aId. No matter what I try, my JSON property is always converted to lowercase (aid).

I tested my same code with abId, and it works... Anyone a clue, where my configuration is wrong or which convention(?) is against this property name?

//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
 }]

I tried with following Annotations:

  • @SerializedName("aId")

  • @JsonProperty("aId")

  • @JsonRawValue

  • @JsonAlias

longi
  • 11,104
  • 10
  • 55
  • 89
  • 1
    Try to check [Usage of Jackson @JsonProperty annotation for kotlin data classes](https://stackoverflow.com/questions/47982148/usage-of-jackson-jsonproperty-annotation-for-kotlin-data-classes) – Michał Ziober Jul 18 '19 at 21:26
  • @MichałZiober thanks dude, that did the trick! post this as an answer to grap the bounty points – longi Jul 19 '19 at 11:15
  • 1
    I am not sure, I should receive them for a linking to another great answer. StackOverflow does not like answers for duplicates, so, I think, some admins should mark your question as duplicated and point to linked question/answer. If you really would like to thank me, please, upvote some my answers related with `Jackson` or `JSON`, if you think they deserve it, of course. – Michał Ziober Jul 19 '19 at 11:21

2 Answers2

2

Try this @get:JsonProperty("aId")

Amroun
  • 415
  • 3
  • 13
  • Oh sorry I didn't check the comments above. I faced the same problem in my project so I already knew the answer. – Amroun Jul 19 '19 at 11:41
2

See Michael Ziober' posted link for answer Usage of Jackson @JsonProperty annotation for kotlin data classes

Described issue is a result of Jackson's default bahaviour to not scan private fields. This behaviour can be change with @JsonAutoDetect

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
data class MyClass(
   @JsonProperty
   val aId: String? = null, 
   @JsonProperty
   val abId: String? = null 
)
longi
  • 11,104
  • 10
  • 55
  • 89