Springboot API 返回空响应。

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

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

  1. @Test
  2. fun test() {
  3. try {
  4. val response = client.submit(request)
  5. if (response.statusCodeValue == 200) {
  6. logger.info("Success")
  7. return
  8. } else {
  9. throw Exception("Internal Server Error")
  10. }
  11. } catch (e: Exception) {
  12. throw Exception("Exception occurred while calling the submit API", e)
  13. }
  14. }
英文:

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

  1. @Bean
  2. fun buildClient(
  3. objectMapper: ObjectMapper,
  4. @Value("${cbase-uri}") url: String
  5. ): Client {
  6. return FeignApiBuilder.builder(Client::class.java, url) { it }
  7. .build()
  8. }

API interface

  1. @Headers(
  2. "Accept: ${MimeTypeUtils.APPLICATION_JSON_VALUE}",
  3. "Content-Type: ${MimeTypeUtils.APPLICATION_JSON_VALUE}"
  4. )
  5. interface Client {
  6. @RequestLine("POST /v1/test")
  7. fun submit(
  8. request: body
  9. ): ResponseEntity<Unit>
  10. }

Implementation

  1. override fun test(request: body) {
  2. try {
  3. val response = client.submit(request)
  4. if (response.statusCodeValue == 200) {
  5. logger.info("Success")
  6. return
  7. } else {
  8. throw Exception("Internal Server Error")
  9. }
  10. } catch (e: Exception) {
  11. throw Exception("Exception occurred while calling submit api", e)
  12. }
  13. }

答案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:

确定