Springboot API 返回空响应。

huangapple go评论77阅读模式
英文:

Springboot API returns null response

问题

This API works fine; I can see it hits the server and returns 200. But the response captured in the client code is null. I have checked the response via postman and I can see the status code. Not Sure why the client is not able to decode the response

API interface

Implementation

@Test
fun test() {
    try {
        val response = client.submit(request)
        if (response.statusCodeValue == 200) {
            logger.info("Success")
            return
        } else {
            throw Exception("Internal Server Error")
        }
    } catch (e: Exception) {
        throw Exception("Exception occurred while calling the submit API", e)
    }
}
英文:

This API works fine; I can see it hits the server and returns 200. But the response captured in the client code is null. I have checked the response via postman and I can see the status code. Not Sure why client is not able to decode the response

@Bean
        fun buildClient(
            objectMapper: ObjectMapper,
            @Value("${cbase-uri}") url: String
        ): Client {
            return FeignApiBuilder.builder(Client::class.java, url) { it }
                .build()
        }

API interface

@Headers(
    "Accept: ${MimeTypeUtils.APPLICATION_JSON_VALUE}",
    "Content-Type: ${MimeTypeUtils.APPLICATION_JSON_VALUE}"
)
interface Client {
    @RequestLine("POST /v1/test")
    fun submit(
        request: body
    ): ResponseEntity<Unit>
}

Implementation

override fun test(request: body) {
        try {
            val response = client.submit(request)
            if (response.statusCodeValue == 200) {
                logger.info("Success")
                return
            } else {
                throw Exception("Internal Server Error")
            }
        } catch (e: Exception) {
            throw Exception("Exception occurred while calling submit api", e)
        }
    }

答案1

得分: 1

问题似乎是由API接口方法的返回类型引起的。您正在返回"ResponseEntity<Unit>",您可以将返回类型更改为预期的类型,然后检查是否解决了问题。

英文:

Looks like the issue is due to return type of API interface method. You are returning "ResponseEntity<Unit>", can you change the return type to the expected type and check if that fixes the issue

huangapple
  • 本文由 发表于 2023年4月4日 10:47:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定