Spring似乎忽略了Hibernate5Module和hibernate对象的延迟加载
异常
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:没有找到类 org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor 的序列化器,并且没有发现创建 BeanSerializer 的属性(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链: java.util.ArrayList[0]->challenge.ChallengeAttempt["user"]->user.User$HibernateProxy$EvJuQSUg["hibernate_lazy_initializer"])
我的实体具有 ManyToOne
关系,如下所示:
@Entity
public class ChallengeAttempt{
//rest of the fields and setters/getters
//....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private User user;
}
这是我的控制器类,它获取数据列表:
@RestController
public class MyController{
@GetMapping("attempts")
public ResponseEntity<List<ChallengeAttempt>> getStatistics(@RequestParam(name = "alias") String alias) {
return ResponseEntity.ok(challengeService.getStatsForUser(alias));
}
}
然后我添加了一个配置来解决延迟加载异常(空失败bean):
@Configuration
public class JsonConfiguration {
@Bean
public Module hibernate5Module() {
Hibernate5Module module = new Hibernate5Module();
return module;
}
}
问题是,当我发送 get 请求来获取最新尝试时(控制器 getStatistics 方法),我仍然收到异常,就好像 JsonConfiguration 不存在一样。 (我已将此类放在主 SptringBootApplication 类下,因此 Spring 实际上正在扫描它。)
更新: 我意识到 Spring 不会自动将 Hibernate5Module 注册到 objectMapper 中。使用 ApplicationContext 我确保 hibernate5Module 已成功加载。然后,当我检查 objectMapper bean 中注册的模块时,我注意到它是空的(意味着没有注册任何模块)。 通过在 objectMapper 中注册 hibernate5Module bean,如下所示,我可以使 Spring 正常工作:
objectMapper.registerModule(hibernate5Module);
但是,我认为这只是一个 hack,根本不推荐。那么,为什么Spring不自动注册hibernate5Module呢?这是 Spring 的一个错误吗?
Exception
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->challenge.ChallengeAttempt["user"]->user.User$HibernateProxy$EvJuQSUg["hibernate_lazy_initializer"])
My entity has a ManyToOne
relationship as follows:
@Entity
public class ChallengeAttempt{
//rest of the fields and setters/getters
//....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private User user;
}
And this is my contorller class which fetches a list of data:
@RestController
public class MyController{
@GetMapping("attempts")
public ResponseEntity<List<ChallengeAttempt>> getStatistics(@RequestParam(name = "alias") String alias) {
return ResponseEntity.ok(challengeService.getStatsForUser(alias));
}
}
Then I added a configuration to solve the lazy loading exception (fail on empty bean):
@Configuration
public class JsonConfiguration {
@Bean
public Module hibernate5Module() {
Hibernate5Module module = new Hibernate5Module();
return module;
}
}
The problem is that when I send a get request to fetch latest attempts (controller getStatistics
method) I still get the exception as if JsonConfiguration didn't exist. (I have put this class under the main SptringBootApplication class, so it is actually being scanned by the Spring.)
UPDATE:
I realized that Spring does not automatically register the Hibernate5Module into objectMapper. Using ApplicationContext I made sure that the hibernate5Module is successfully loaded. Then, when I checked the registered modules in objectMapper bean, I noticed it is empty (Meaning that no module has been registered).
By registering hibernate5Module bean in objectMapper as follows I could make the Spring to work properly:
objectMapper.registerModule(hibernate5Module);
However, I think it's just a hack and is not recommended at all. So, why doesn't Spring register hibernate5Module automatically? Is this a bug in Spring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论