Strange Jackson exception being thrown when serializing Hibernate object

前端 未结 15 2742
离开以前
离开以前 2020-11-29 18:12

Jackson is throwing a weird exception that I don\'t know how to fix. I\'m using Spring, Hibernate and Jackson.

I have already considered that lazy-loading is causing

15条回答
  •  爱一瞬间的悲伤
    2020-11-29 19:14

    I tried @JsonDetect and

    @JsonIgnoreProperties(value = { "handler", "hibernateLazyInitializer" })

    Neither of them worked for me. Using a third-party module seemed like a lot of work to me. So I just tried making a get call on any property of the lazy object before passing to jackson for serlization. The working code snippet looked something like this :

    @RequestMapping(value = "/authenticate", produces = "application/json; charset=utf-8")
        @ResponseBody
        @Transactional
        public Account authenticate(Principal principal) {
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) principal;
            LoggedInUserDetails loggedInUserDetails = (LoggedInUserDetails) usernamePasswordAuthenticationToken.getPrincipal();
            User user = userRepository.findOne(loggedInUserDetails.getUserId());
            Account account = user.getAccount();
            account.getFullName();      //Since, account is lazy giving it directly to jackson for serlization didn't worked & hence, this quick-fix.
            return account;
        }
    

提交回复
热议问题