我已经看过一些关于此的帖子,但似乎我一直遇到同样的问题
这是一个类似的帖子:
JsonMappingException: could not initialize proxy - no Session
问题
所以建议添加以下内容:
private MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
//Registering Hibernate4Module to support lazy objects
mapper.registerModule(new Hibernate5Module());
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}
但我在super.configureMessageConverters()
内没有super
方法。他们说简单的解决方案是添加EAGER
,但不是首选。另一个解决方案是使用@JsonIgnore
,但我想检索相应的实体。据说,添加您在本文上方看到的代码是处理此问题的正确和干净方法之一。这仍然是真的吗?如果是这样,那么我应该如何写super.configureMessageConverters(converters);
,因为这似乎是问题所在。
额外信息
当我添加多对多关系时出现此问题:
类型
// region: relationships
@ManyToMany(mappedBy = "types")
private Set<License> licenses;
// endregion: relationships
许可证
@ManyToMany
@JoinTable(name = "license_restrictions",
joinColumns = @JoinColumn(name = "license_id"),
inverseJoinColumns = @JoinColumn(name = "type_id"))
private Set<Type> types;
// endregion: relationships
EDITED V1
我已根据评论中的建议添加了以下内容:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jackson2HttpMessageConverter());
}
@Bean
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(this.jacksonBuilder().build());
return converter;
}
private Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
Hibernate5Module hibernateModule = new Hibernate5Module();
hibernateModule.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, false);
builder.modules(hibernateModule);
// Spring MVC default Objectmapper configuration
builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
builder.featuresToDisable(MapperFeature.DEFAULT_VIEW_INCLUSION);
return builder;
}
这也不会返回结果。
类型是null
,而它应该是2种类型的集合。
在这里,您可以看到我确实拥有绑定到许可证1的类型。