Strange Jackson exception being thrown when serializing Hibernate object

前端 未结 15 2704
离开以前
离开以前 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:12

    For what it's worth, there is Jackson Hibernate module project that just started, and which should solve this problem and hopefully others as well. Project is related to Jackson project, although not part of core source. This is mostly to allow simpler release process; it will require Jackson 1.7 as that's when Module API is being introduced.

    0 讨论(0)
  • 2020-11-29 19:14

    You can use jackson-datatype-hibernate module to solve this problem. It work for me. reference: https://github.com/FasterXML/jackson-datatype-hibernate

    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
提交回复
热议问题