简体   繁体   English

如何在没有弹簧关系的情况下获取序列化对象

[英]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.

相关问题 将序列化对象从客户端传递到Spring控制器并返回序列化对象响应的最简单方法? - Simplest way to pass a Serialized object from client to Spring controller and get back serialized object response? 如何获取序列化的tomcat HttpSession以重新填充Spring SessionRegistry - How to get serialized tomcat HttpSession to repopulate spring SessionRegistry Spring 引导:如何从 FieldError.getField() 获取序列化名称 - Spring Boot: How to get the Serialized Name from FieldError.getField() 杰克逊序列化器:获取序列化的对象 - jackson serializer: get serialized object 如何获取要序列化并发送到客户端的Java对象字段 - How to get Java object field to be serialized and sent to the client 如何从文件中获取序列化 object 的特定实例? - How to get a particular instance of serialized object from file? 如何获取表示Java对象的序列化字节数? - How to get amount of serialized bytes representing a Java object? Spring Boot如何序列化对象 - How spring boot serialized objects 当object需要在spring开机序列化 - When object needs to be serialized in spring boot Java对象序列化而不实现可序列化接口 - Java object serialized without implementing serializable interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM