fuel-jackson

The jackson extension package for Fuel.

Installation

You can download and install fuel-jackson with Maven and Gradle. The jackson package has the following dependencies:

  1. implementation 'com.github.kittinunf.fuel:fuel:<latest-version>'
  2. implementation 'com.github.kittinunf.fuel:fuel-jackson:<latest-version>'

Usage

The Fuel-Jackson module provides a built in support for Jackson deserialization.This is done by adding the responseObject extension function into Fuel Request interface.

By default, the responseObject call will use the defaultMapper property defined in FuelJackson.kt.

  1. data class HttpBinUserAgentModel(var userAgent: String = "")
  2.  
  3. Fuel.get("/user-agent")
  4. .responseObject<HttpBinUserAgentModel>()

Alternatively, you can provide your own ObjectMapper as a parameter to it.

  1. data class HttpBinUserAgentModel(var userAgent: String = "")
  2.  
  3. val mapper = ObjectMapper().registerKotlinModule()
  4. .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  5.  
  6. mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
  7.  
  8. Fuel.get("/user-agent")
  9. .responseObject<HttpBinUserAgentModel>(mapper)

Also, the responseObject overloads allows you to pass Response Handlers as lambda functions

  1. data class HttpBinUserAgentModel(var userAgent: String = "")
  2.  
  3. Fuel.get("/user-agent").responseObject<HttpBinUserAgentModel> { request, response, result ->
  4. //handle here
  5. }

or ResponseHandler<T> instances.

  1. data class HttpBinUserAgentModel(var userAgent: String = "")
  2.  
  3. Fuel.get("/user-agent")
  4. .responseObject(object : ResponseHandler<HttpBinUserAgentModel> {
  5. override fun success(request: Request, response: Response, value: HttpBinUserAgentModel) {
  6. //handle success
  7. }
  8.  
  9. override fun failure(request: Request, response: Response, error: FuelError) {
  10. //handle failure
  11. }
  12. })

Both overloads allows you to provide custom ObjectMapper if needed.