首页 > 解决方案 > 我在邮递员和浏览器中收到 500 错误,但控制器在调试时返回正确的对象

问题描述

我编写了 API,当通过邮递员或浏览器点击时会导致 500 错误。但是,当我调试并看到服务器没有抛出任何错误并且实际上返回了正确的响应时。我以类似方式实现的其他控制器正在返回预期结果。下面是我的控制器代码。有没有人遇到过类似的情况。请帮忙。

    @CrossOrigin
    @GetMapping(value="/byPatientId/{patientId}", produces = "application/json")
    public List<ContactInfo> getAllContacts(@PathVariable String patientId) {
        logger.info("Received request for List of ContactInfo for patientId: "+patientId);
        List<ContactInfo> list = 
         contactInfoService.getAllContacts(patientId);
        return list;
    }

    @CrossOrigin
    @GetMapping("/byContactId/{contactId}")
    public ContactInfo getContactById(@PathVariable Integer contactId) {
        logger.info("Received request for ContactInfo for contactId: "+contactId);
        return contactInfoService.getContactById(contactId);
    }

标签: javaspring-boothttp-status-code-500rest

解决方案


问题在于与返回类型对象具有 oneToMany 关系的依赖对象之一。它被设置为延迟加载,因此在将对象序列化为 JSON 时,可能存在一些问题。我通过在依赖对象之上添加“@JsonIgnore”注释来处理它。现在问题解决了。


推荐阅读