Spring 当jackson序列化json对象时,如何防止hibernate5延迟抓取?
我目前正在运行一个spring引导应用程序,其中端点返回存储在数据库中的特定对象的页面。为了我们的目的,让我们把这个对象称为“x”。在“x”中有一个设置为延迟获取的对象列表Spring 当jackson序列化json对象时,如何防止hibernate5延迟抓取?,spring,hibernate,spring-boot,jackson,hibernate-5.x,Spring,Hibernate,Spring Boot,Jackson,Hibernate 5.x,我目前正在运行一个spring引导应用程序,其中端点返回存储在数据库中的特定对象的页面。为了我们的目的,让我们把这个对象称为“x”。在“x”中有一个设置为延迟获取的对象列表 @Entity @DynamicUpdate class x { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @JsonIgnore @OneToMany
@Entity
@DynamicUpdate
class x {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@JsonIgnore
@OneToMany(mappedBy = "x", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private List<y> lazilyFetchedList;
@Override
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JsonIgnore
public List<y> getLazilyFetchedList() {
return lazilyFetchedList;
}
public void setLazilyFetchedList(List<y> lazilyFetchedList) {
this.lazilyFetchedList = lazilyFetchedList;
}
}
以下是我对对象映射器的配置:
@Configuration
@EnableWebMvc
public class ApiWebConfig extends WebMvcConfigurerAdapter {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
configureDefaultObjectMapper(objectMapper);
customizeObjectMapper(objectMapper);
return objectMapper;
}
public static void configureDefaultObjectMapper(ObjectMapper objectMapper) {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
objectMapper.registerModule(new Hibernate5Module());
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE);
javaTimeModule.addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE);
objectMapper.registerModule(javaTimeModule);
}
/**
* Only register a json message converter
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.clear();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON));
converter.setObjectMapper(objectMapper());
converters.add(converter);
}
}
@配置
@EnableWebMvc
公共类ApiWebConfig扩展了WebMVCConfigureAdapter{
@豆子
公共对象映射器对象映射器(){
ObjectMapper ObjectMapper=新的ObjectMapper();
配置DefaultObjectMapper(objectMapper);
自定义对象映射器(objectMapper);
返回对象映射器;
}
公共静态void配置DefaultObjectMapper(ObjectMapper ObjectMapper){
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_案例);
configure(在未知属性上反序列化feature.FAIL,true);
configure(SerializationFeature.WRITE_DATES_作为时间戳,false);
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
registerModule(新的Hibernate5Module());
JavaTimeModule JavaTimeModule=新的JavaTimeModule();
addSerializer(ZonedDateTime.class,ZonedDateTimeSerializer.INSTANCE);
addSerializer(OffsetDateTime.class,OffsetDateTimeSerializer.INSTANCE);
registerModule(javaTimeModule);
}
/**
*仅注册json消息转换器
*/
@凌驾
public void configureMessageConverters(列表将以下依赖项添加到pom.xml中:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>
使用以下导入:
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
如果删除getter/setter会发生什么?不要使用实体类,而是为json创建pojo。问题是,我的项目中有很多实体。因此为每个实体创建pojo不是可行的解决方案,而且我不想实现一个需要在两个不同位置更改字段的解决方案,因为很有可能如果有人更新那个实体,代码就会被破坏。我想要一个由库自己处理的更通用的解决方案。
@Bean
protected Module module() {
return new Hibernate5Module();
}
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;