Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jackson 关联关系序列化常用的注解,解决死循环,JSON常见的异常问题 #49

Open
zhangzhenhuajack opened this issue Jul 12, 2022 · 1 comment

Comments

@zhangzhenhuajack
Copy link
Owner

这报错就一直报 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

@zhangzhenhuajack
Copy link
Owner Author

循环引用问题:@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

@zhangzhenhuajack zhangzhenhuajack changed the title jackson 关联关系序列化常用的注解,解决死循环,场景的异常问题 jackson 关联关系序列化常用的注解,解决死循环,JSON常见的异常问题 Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant