Jackson

ContentNegotiation provides the built-in Jackson converter for handing JSON data in your application.

Add Dependencies

Before registering the Jackson converter, you need to include the following artifacts in the build script:

Jackson - 图1

Gradle (Groovy)

Gradle (Kotlin)

Maven

Jackson - 图2

  1. implementation "io.ktor:ktor-jackson:$ktor_version"

Register the Jackson Converter

To register the Jackson converter in your application, call the jackson method:

  1. import io.ktor.jackson.*
  2. install(ContentNegotiation) {
  3. jackson()
  4. }

Inside the jackson block, you can access the ObjectMapper API, for example:

  1. install(ContentNegotiation) {
  2. jackson {
  3. enable(SerializationFeature.INDENT_OUTPUT)
  4. dateFormat = DateFormat.getDateInstance()
  5. // ...
  6. }
  7. }

To learn how to receive and send data, see Receive and Send Data.