错误:异常[处理程序调度失败;嵌套异常是 java.lang.StackOverflowError] 其根本原因

问题描述 投票:0回答:1

当 jpa 尝试从数据库获取对象时出现此错误。

错误:路径[/api]上下文中servlet [dispatcherServlet]的Servlet.service()抛出异常[处理程序调度失败;嵌套异常是 java.lang.StackOverflowError] 其根本原因

AdministrateurController 在此 selectAllAdmin 方法不起作用并导致错误

@RestController
@RequestMapping("/admins")
public class AdministrateurController {
    public static final Logger LOGGER = LoggerFactory.getLogger(AdministrateurController.class);
    private AdministrateurService administrateurService;

    public AdministrateurController(@Qualifier("AdministrateurService") AdministrateurService administrateurService) {
        this.administrateurService = administrateurService;
    }
    
    //Methode selectAllAdmin
    @GetMapping("selectAllAdmin")
    List<AdministrateurDto> selectAllAdmin(){
        LOGGER.debug("Start methode selectAllAdmin");
        try {
            return administrateurService.selectAllAdmin();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

管理员服务

@Service("AdministrateurService")
public class AdministrateurServiceImlp implements  AdministrateurService{
    public static final Logger LOGGER = LoggerFactory.getLogger(AdministrateurServiceImlp.class);
    private AdministrateurMapper administrateurMapper = Mappers.getMapper(AdministrateurMapper.class);
    private AdministrateurRepo administrateurRepo;

    public AdministrateurServiceImlp(AdministrateurRepo administrateurRepo) {
        this.administrateurRepo = administrateurRepo;
    }
    

    @Override
    public List<AdministrateurDto> selectAllAdmin() {
        LOGGER.debug("Start methode selectAllAdmin");
        try {
            return administrateurMapper.listAdminEntityToDto(administrateurRepo.findAll());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

**管理实体 **

@Entity
@Table(name = "Administration")
public class Administrateur {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer idAdmin;
    @Column(nullable = false, unique = true,length = 10)
    private String cin;
    @Column(name = "nomComplet", nullable = false)
    private String nom;

    @OneToOne
    @JoinColumn(nullable = false,name = "idUser")
    private Utilisateur user;
    @OneToMany(mappedBy = "admin")
    private List<Client> clientList;

    public Integer getIdAdmin() {
        return idAdmin;
    }

    public void setIdAdmin(Integer idAdmin) {
        this.idAdmin = idAdmin;
    }

    public String getCin() {
        return cin;
    }

    public void setCin(String cin) {
        this.cin = cin;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public Utilisateur getUser() {
        return user;
    }

    public void setUser(Utilisateur user) {
        this.user = user;
    }

    public List<Client> getClientList() {
        return clientList;
    }

    public void setClientList(List<Client> clientList) {
        this.clientList = clientList;
    }
}

这是我的终端

2024-02-25 13:27:51,246 DEBUG [http-nio-8086-exec-7] o.s.c.l.LogFormatUtils: GET "/api/admins/selectAllAdmin", parameters={}
2024-02-25 13:27:51,247 DEBUG [http-nio-8086-exec-7] o.s.w.s.h.AbstractHandlerMapping: Mapped to com.appGS.controllers.AdministrateurController#selectAllAdmin()
2024-02-25 13:27:51,253 DEBUG [http-nio-8086-exec-7] c.a.c.AdministrateurController: Start methode selectAllAdmin
2024-02-25 13:27:51,254 DEBUG [http-nio-8086-exec-7] c.a.s.AdministrateurServiceImlp: Start methode selectAllAdmin
Hibernate: select administra0_.id_admin as id_admin1_0_, administra0_.cin as cin2_0_, administra0_.nom_complet as nom_comp3_0_, administra0_.id_user as id_user4_0_ from administration administra0_
Hibernate: select utilisateu0_.user as user1_5_0_, utilisateu0_.email as email2_5_0_, utilisateu0_.mot_de_passe as mot_de_p3_5_0_, administra1_.id_admin as id_admin1_0_1_, administra1_.cin as cin2_0_1_, administra1_.nom_complet as nom_comp3_0_1_, administra1_.id_user as id_user4_0_1_ from utilisater utilisateu0_ left outer join administration administra1_ on utilisateu0_.user=administra1_.id_user where utilisateu0_.user=?
Hibernate: select administra0_.id_admin as id_admin1_0_1_, administra0_.cin as cin2_0_1_, administra0_.nom_complet as nom_comp3_0_1_, administra0_.id_user as id_user4_0_1_, utilisateu1_.user as user1_5_0_, utilisateu1_.email as email2_5_0_, utilisateu1_.mot_de_passe as mot_de_p3_5_0_ from administration administra0_ inner join utilisater utilisateu1_ on administra0_.id_user=utilisateu1_.user where administra0_.id_user=?
Hibernate: select utilisateu0_.user as user1_5_0_, utilisateu0_.email as email2_5_0_, utilisateu0_.mot_de_passe as mot_de_p3_5_0_, administra1_.id_admin as id_admin1_0_1_, administra1_.cin as cin2_0_1_, administra1_.nom_complet as nom_comp3_0_1_, administra1_.id_user as id_user4_0_1_ from utilisater utilisateu0_ left outer join administration administra1_ on utilisateu0_.user=administra1_.id_user where utilisateu0_.user=?
Hibernate: select administra0_.id_admin as id_admin1_0_1_, administra0_.cin as cin2_0_1_, administra0_.nom_complet as nom_comp3_0_1_, administra0_.id_user as id_user4_0_1_, utilisateu1_.user as user1_5_0_, utilisateu1_.email as email2_5_0_, utilisateu1_.mot_de_passe as mot_de_p3_5_0_ from administration administra0_ inner join utilisater utilisateu1_ on administra0_.id_user=utilisateu1_.user where administra0_.id_user=?
Hibernate: select utilisateu0_.user as user1_5_0_, utilisateu0_.email as email2_5_0_, utilisateu0_.mot_de_passe as mot_de_p3_5_0_, administra1_.id_admin as id_admin1_0_1_, administra1_.cin as cin2_0_1_, administra1_.nom_complet as nom_comp3_0_1_, administra1_.id_user as id_user4_0_1_ from utilisater utilisateu0_ left outer join administration administra1_ on utilisateu0_.user=administra1_.id_user where utilisateu0_.user=?
Hibernate: select administra0_.id_admin as id_admin1_0_1_, administra0_.cin as cin2_0_1_, administra0_.nom_complet as nom_comp3_0_1_, administra0_.id_user as id_user4_0_1_, utilisateu1_.user as user1_5_0_, utilisateu1_.email as email2_5_0_, utilisateu1_.mot_de_passe as mot_de_p3_5_0_ from administration administra0_ inner join utilisater utilisateu1_ on administra0_.id_user=utilisateu1_.user where administra0_.id_user=?
2024-02-25 13:27:51,714 DEBUG [http-nio-8086-exec-7] o.s.w.s.FrameworkServlet: Failed to complete request: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
2024-02-25 13:27:51,721 ERROR [http-nio-8086-exec-7] o.a.j.l.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
    at com.appGS.mappers.AdministrateurMapperImpl.utilisateurToUtilisateurDto(AdministrateurMapperImpl.java:181)
    at com.appGS.mappers.AdministrateurMapperImpl.adminEntityToDto(AdministrateurMapperImpl.java:52)
    at com.appGS.mappers.AdministrateurMapperImpl.utilisateurToUtilisateurDto(AdministrateurMapperImpl.java:184)

spring spring-boot spring-mvc spring-data-jpa spring-data
1个回答
0
投票
  1. 对于初学者,您可以在这两个关系上使用延迟获取

    @OneToOne @JoinColumn(nullable = false,name = "idUser") private Utilisateur user;
    @OneToMany(mappedBy = "admin") private List<Client> clientList;

    原因是,由于对客户端表的多次调用可能会导致 StackOverflowError。

  2. administrateurRepo.findAll()
    的结果获取到变量,而不是直接在方法调用中使用。

使用

列出管理员 = administrateurRepo.findAll(); administrateurMapper.listAdminEntityToDto(管理员);

而不是

administrateurMapper.listAdminEntityToDto(administrateurRepo.findAll());

  1. 检查方法
    administrateurMapper.listAdminEntityToDto(administrateurRepo.findAll());
    是否存在无限循环/递归。
© www.soinside.com 2019 - 2024. All rights reserved.