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

I am getting 500 error in postman and browser but the controller is returning correct object when in debug

我已经写了 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);
    }

你的控制器是如何注释的?是@Controller 还是@Rest?

  • @RestController = @Controller + @ResponseBody(用于序列化响应并将其传递到 HttpResponse。

在控制器的方法中添加@ResponseBody 或将@Controller 标记更改为@RestController(考虑到@RestController 从 4.0 Spring 版本开始可用)。

更多info:https://www.baeldung.com/spring-controller-vs-restcontroller

问题出在与 return 类型对象具有一对多关系的依赖对象之一。它被设置为延迟加载,因此在将对象序列化为 JSON 时,可能会出现一些问题。我通过在依赖对象的顶部添加“@JsonIgnore”注释来处理它。问题现已解决。