【问题标题】:Response returned from my Spring Boot Rest API is not same as the backend API provided to my rest API从我的 Spring Boot Rest API 返回的响应与提供给我的 rest API 的后端 API 不同
【发布时间】:2020-04-02 13:35:43
【问题描述】:

在我们的 Spring Boot 应用程序中,我们调用了一个第三方的 rest API,它会以下面的格式返回响应数据:

找到数据后将得到以下响应:

  {
    "items": [
        {
            "eventType": "ABC",
            "timestamp": "01-01-2020"
        },
        {
            "eventType": "XYZ",
            "timestamp": "02-02-2020"
        }
    ]
}

当没有找到数据或为空时将得到以下响应:

  {
    "items": []
}

实际上,我们的rest api在成功时会返回正确格式的数据(找到数据)。但是当没有找到数据(空数据)时,我们的 rest api 会返回这样的“{}”而不是“{'items':[]}”不正确。

在控制器中,我们调用服务类方法:

    @Autowired
    private ProductService productService;

    @GetMapping("/getProduct/{productNo}")
    public ResponseEntity<Product> getProduct(@PathVariable("productNo") String productNo) {
        logger.debug("--- getProduct() Method Called ---");
        return ResponseEntity.ok(productService.getProduct(productNo));
    }

在服务类中,我们调用后端 url 使用:

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Product getProduct(String productNo) {
        return restTemplate.getForObject("BACK_END_URL/" + productNo, Product.class);
    }

请让我们知道我们是否在这里遗漏了什么或做错了什么。

提前致谢:)

【问题讨论】:

  • 嗨,Jay,你能告诉我产品类吗? @Jay
  • 我们的第三方应用程序正在向我们返回以下响应:{ "items": [] } 但是在我们收到它之后,我们将放入相应的对象并将响应返回给调用 api。以下是我们收到的错误回复:{}
  • 但是您必须正确设置 Product 类。你能展示一下吗?
  • @CodeWalter,请找到产品类: public class Product { private List items = new ArrayList(); public Product() { } public List getItems() { return items; } public void setItems(List items) { this.items = items; } }

标签: java spring spring-boot rest


【解决方案1】:

如果您想在items 为空时获取[],请初始化items 列表并更改Product 类中的items 设置器,如下所示:

import java.util.ArrayList;
import java.util.List;

public class Product {

    private List<Item> items = new ArrayList<>();

    public Product() {
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        if (items != null) {
            this.items = items;
        }
    }
}

结果:

{
    "items": []
}

【讨论】:

  • Product 类中添加这行代码 private List&lt;Item&gt; items = new ArrayList&lt;&gt;(); 而不是 private List&lt;Item&gt; items; 并再次测试。
  • 感谢@Nasir,但我们没有做出任何回应。你提到的方式我们都知道。但我们想知道,如何传递来自第三方的空响应。在服务类中,我们调用第三方 url,如下所示: public Product getProduct(String productNo) { Product prod = restTemplate.getForObject("BACK_END_URL/" + productNo, Product.class);返回产品;当我们调用“restTemplate.getForObject()”然后放入相应的对象并返回它时,将得到空响应。
  • 你能把你的Product类放在问题中吗?
  • 请找到产品类: public class Product { private List items = new ArrayList(); public Product() { } public List getItems() { return items; } public void setItems(List items) { this.items = items; } }
  • @Jay,我更改了答案并更改了items 设置器并添加了if
【解决方案2】:

试试这个产品类

public class Product {
     private List<Item> items;

     public Product() {
     }

     public Product(List<Item> items) {
       super();
       this.items = items;
    }



    public List<Item> getItems() {
       return items;
    }

    public void setItems(List<Item> items) {
       this.items = items;
    }
}

【讨论】:

  • 嗨@CodeWalter,我改变了你提到的产品类,但仍然是同样的问题,我们仍然得到“{}”而不是“{'items':[]}”:(跨度>
  • 您应该在 getForObject() 方法中进行调试以获得确切的原因。
猜你喜欢
  • 2022-01-23
  • 2019-07-29
  • 1970-01-01
  • 2020-02-24
  • 2017-06-27
  • 2021-06-05
  • 2018-03-30
  • 1970-01-01
  • 2019-02-18
相关资源
最近更新 更多