Retrofit2 + SimpleXML in Kotlin: MethodException: Annotation must mark a set or get method

后端 未结 3 1613
清酒与你
清酒与你 2020-12-08 11:03

I want to fetch XML data from API and map it to Kotlin model object by using Retrofit2 + SimpleXML in Kotlin.

However, I got such as the following error message from

3条回答
  •  悲哀的现实
    2020-12-08 11:32

    This is the same problem as: kotlin data class + bean validation jsr 303

    You need to use Annotation use-site targets since the default for an annotation on a property is prioritized as:

    • parameter (if declared in constructor)
    • property (if the target site allows, but only Kotlin created annotations can do this)
    • field (likely what happened here, which isn't what you wanted).

    Use get or set target to place the annotation on the getter or setter. Here it is for the getter:

    @Root(name = "response")
    public class User() {
        @get:Element public var result: String? = null
        @get:Element public var token: String? = null
        @get:Element public var uid: String? = null
    }
    

    See the linked answer for the details.

提交回复
热议问题