spring-data-jpa-guide
spring-data-jpa-guide copied to clipboard
jackson 关联关系序列化常用的注解,解决死循环,JSON常见的异常问题
这报错就一直报 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: com.example.demo.User["role"]
解决方法入下:
1. spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
2. 配置ObjectMapper
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
MappingJackson2HttpMessageConverter converter =
new MappingJackson2HttpMessageConverter(mapper);
return converter;
}
https://stackoverflow.com/questions/28862483/spring-and-jackson-how-to-disable-fail-on-empty-beans-through-responsebody
循环引用问题:@JsonManagedReference and @JsonBackReference
public class User {
public int id;
public String name;
@JsonManagedReference
public List<Item> userItems;
}
public class Item {
public int id;
public String itemName;
@JsonBackReference
public User owner;
}
https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion