简体   繁体   中英

How to use the OpenAPI generators retrofit stub with android kotlin client? Response 501 "Not Implemented"

I want to create a REST-API between an Android client and a Spring Boot server. I created an OpenAPI 3.0 specification and used the CLI generator from https://openapi-generator.tech to create client and server stubs.

The server part works as intended when accessing it with other clients.

For the client side I used the generator for Kotlin with Retrofit2 via the parameter --additional-properties=library=jvm-retrofit2 . What I get is:

  1. A ModelApi interface, defining my endpoint
  2. A Model class, containing my model
  3. An infrastructure package, containing ApiClient, ResponseExt, Serializer, CollectionFormats and a few *Adapter classes

The generated model class (shortened):

data class MapModel (

    @Json(name = "id")
    val id: kotlin.Long? = null,

    @Json(name = "description")
    val desc: String? = null

)

The API interface:

interface MapModelApi {

    @GET("mapModel")
    fun mapModelGet(): Call<kotlin.collections.List<MapModel>>

    @DELETE("mapModel/{mapModelId}")
    fun mapModelMapModelIdDelete(@Path("mapModelId") mapModelId: kotlin.Int): Call<Unit>

    @GET("mapModel/{mapModelId}")
    fun mapModelMapModelIdGet(@Path("mapModelId") mapModelId: kotlin.Int): Call<MapModel>

    @PUT("mapModel/{mapModelId}")
    fun mapModelMapModelIdPut(@Path("mapModelId") mapModelId: kotlin.Int, @Body mapModel: MapModel): Call<Unit>

    @POST("mapModel")
    fun mapModelPost(@Body mapModel: MapModel): Call<Unit>

    @PUT("mapModel")
    fun mapModelPut(@Body mapModel: MapModel): Call<Unit>

}

To do a GET request on the element 0, i tried this in my Activity:

val apiClient = ApiClient()
val mapObjectService = apiClient.createService(MapModelApi::class.java)
val call = mapObjectService.mapModelMapModelIdGet(0)

call.enqueue(object : Callback<MapModel> {
            override fun onFailure(
                call: Call<MapModel>,
                t: Throwable
            ) {
                Log.v("retrofit", "call failed")
                t.printStackTrace()
            }

            override fun onResponse(
                call: Call<MapModel>,
                response: Response<MapModel>
            ) {
                if (response.isSuccessful) {
                    val mapModel = response.body()
                    println(mapModel?.id)
                } else {
                    val statusCode = response.code()
                    println("Http Code: $statusCode")
                }
            }

        })

When I execute this I get a response, but it is always a 501 response "Not Implemented". How can I fix this? What is missing in the code?

The server is the problem. The GET request returned a body with example data. I have overseen, that the request code sent by the server was not 200, but 501.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM