Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit or Jackson ObjectMapper maps "aId" property to lowercase "aid"

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

like image 664
longi Avatar asked Jul 16 '19 13:07

longi


2 Answers

Try this @get:JsonProperty("aId")

like image 109
Amroun Avatar answered Nov 06 '22 06:11

Amroun


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 
)
like image 33
longi Avatar answered Nov 06 '22 06:11

longi