How to get Json Response from RestAPI in kotlin Springboot

113 Views Asked by At

Recently I updated SpringFox to Springdoc open API for swagger-related changes, apparently, I observed that all my API responses are coming in XML format, earlier they were in JSON format. Below are snippets of my build.gradle.kts file for swagger and Jackson dependencies

build.gradele.kt:

id("org.springframework.boot") version "2.7.11"
id("io.spring.dependency-management") version "1.0.15.RELEASE"
kotlin("jvm") version "1.8.0"
kotlin("plugin.spring") version "1.6.21"
id("org.jetbrains.kotlin.plugin.jpa") version "1.6.21"

implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.1")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.1")

implementation("org.springdoc:springdoc-openapi-webmvc-core:1.7.0")
implementation("org.springdoc:springdoc-openapi-ui:1.6.12")
implementation("org.springdoc:springdoc-openapi-data-rest:1.7.0")
implementation("org.springdoc:springdoc-openapi-kotlin:1.7.0")

implementation("com.github.kittinunf.fuel:fuel:2.3.1")
implementation("com.github.kittinunf.fuel:fuel-jackson:2.3.1")

I tried removing implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.1") dependency, it works but it gives me an error for some APIs having XML response.

controller.kt:

@Tag(description = "ABC details API", name = "abc")
@RestController
@RequestMapping("/api/x1")
class detailController(val detailService: DetailService) {


    @Operation(summary = "API to fetch details")
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/product/{id}", produces = ["application/json"], consumes = [MediaType.ALL_VALUE])
    @ApiResponse(responseCode = "200", description = "Ok",
        content =
    [Content(mediaType = "application/json", schema =
        Schema(implementation = DetailsResponseDTO::class))])
    fun fetchDetails(@Parameter(name = "productId", required = true)
                            @PathVariable("productId", required = true) productId:Int
        ): ProductDetailsResponseDTO? {
            return productDetailService.fetchDetails(productId)
        }
}

Please suggest.

0

There are 0 best solutions below