Skip to content Skip to sidebar Skip to footer

Retrofit2 + Simplexml In Kotlin: Methodexception: Annotation Must Mark A Set Or Get Method

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 SimpleXML. or

Solution 1:

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.

Solution 2:

Toavoidtheerrorinparsedooneshouldplaceannotationtags @sete @get

@Root(name = "response", strict = false)
publicclassUser() {

    @set:Element(name = "result")
    @get:Element(name = "result")
    public var result: String? = null

    @set:Element(name = "token")
    @get:Element(name = "token")
    @Element public var token: String? = null

    @set:Element(name = "uid")
    @get:Element(name = "uid")
    public var uid: String? = null
}

Solution 3:

Not familiar with the SimpleXml library but if it's annotation processor is looking for specific get and set methods you may want to try setting up your class in the following way:

@Root(name="response")classUser() {
var result:String?=null@Elementget@Elementsetvar token:String?=null@Elementget@Elementsetvar uid:String?=null@Elementget@Elementset
}

As well, if the @Element annotation supports field types you could use this approach:

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

Post a Comment for "Retrofit2 + Simplexml In Kotlin: Methodexception: Annotation Must Mark A Set Or Get Method"