[英]How to get serialized object without their relationships on spring
I have the next code: 我有下一个代码:
My entities: 我的实体:
@Entity
@NamedEntityGraphs({
@NamedEntityGraph(
name = "client",
attributeNodes = {
@NamedAttributeNode( value = "country" )}),
@NamedEntityGraph(
name = "only_client",
attributeNodes = {})
})
public class Client{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ClientId")
private int id;
private String firstName;
private String lastName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CountryId")
private Country country;
//Constructor, getters and setters...
}
//Country class...
My repository: 我的资料库:
@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Serializable> {
// #1
@Override
@EntityGraph(value = "client",type = EntityGraph.EntityGraphType.FETCH)
List<Cliente> findAll();
// #2
@EntityGraph(value = "only_client")
List<Cliente> findAllByLastNameContaining(String lastName);
}
So, when I use the number one method works fine but when I try the number two the console throwing: 因此,当我使用第一方法时,效果很好,但是当我尝试第二方法时,控制台将抛出:
Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS);
无法编写JSON:没有为类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer找到序列化器,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.tfasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.spring.demo.entity.Client["country"]->com.spring.demo.entity.Country_$$_jvst9a4_1["handler"])
嵌套的异常是com.tfasterxml.jackson.databind.JsonMappingException:没有为org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer类找到序列化器,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链) :java.util.ArrayList [0]-> com.spring.demo.entity.Client [“ country”]-> com.spring.demo.entity.Country _ $$ _ jvst9a4_1 [“ handler”])
I understand that jackson tried to serialize country bean but my purpose is get only the main parameters of Client not their relationships. 我了解杰克逊曾尝试序列化国家/地区bean,但我的目的是仅获取Client的主要参数,而不获取其关系。
PD: I've already does that what console request me: PD:我已经做了控制台要求我执行的操作:
ObjectMapper objMapper = new ObjectMapper();
objMapper .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return objMapper .writeValueAsString(listOnlyClient)
And this works but as the first method therefore isn't a option. 这是可行的,但是作为第一种方法,因此不是一种选择。
Thanks for advance. 感谢前进。
Try adding this: 尝试添加以下内容:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate5Module());
According to your version hibernate pick Hibernate5Module/4 or 3
根据您的版本休眠挑
Hibernate5Module/4 or 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.