【问题标题】:Jackson exception during serialization序列化期间的杰克逊异常
【发布时间】:2012-11-07 13:32:18
【问题描述】:

我遇到过这个问题,有点接近this issue,但是当我完成所有步骤后,我仍然有这样的异常:

org.codehaus.jackson.map.JsonMappingException:找不到序列化程序 org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer 类和 没有发现创建 BeanSerializer 的属性(为了避免异常, 禁用 SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )(通过 参考链: java.util.ArrayList[0]->com.myPackage.SomeEntity["mainEntity"]->com.myPackage.MainEntity["subentity1"]->com.myPackage.Subentity1_$$_javassist_8["handler"])

这是我的实体的代码:

@JsonAutoDetect
public class MainEntity {

    private Subentity1 subentity1;

    private Subentity2 subentity2;

    @JsonProperty
    public Subentity1 getSubentity1() {
        return subentity1;
    }

    public void setSubentity1(Subentity1 subentity1) {
        this.subentity1 = subentity1;
    }

    @JsonProperty
    public Subentity2 getSubentity2() {
        return subentity2;
    }

    public void setSubentity2(Subentity2 subentity2) {
        this.subentity2 = subentity2;
    }
}



@Entity
@Table(name = "subentity1")
@JsonAutoDetect
public class Subentity1 {

    @Id
    @Column(name = "subentity1_id")
    @GeneratedValue
    private Long id;

    @Column(name = "name", length = 100)
    private String name;

    @JsonIgnore
    @OneToMany(mappedBy = "subentity1")
    private List<Subentity2> subentities2;

     @JsonProperty
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JsonProperty
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //here I didin't add @JsonProperty, cause it leads to cycling during serialization
    public List<Subentity2> getSubentity2s() {
        return subentity2s;
    }

    public void setSubentity2s(List<Subentity2> subentity2s) {
        this.subentity2s = subentity2s;
    }

}

@Entity
@Table(name = "subentity2")
@JsonAutoDetect
public class Subentity2 {
    @Id
    @Column(name = "subentity2_id")
    @GeneratedValue
    private Long id;

    @Column(name = "name", length = 50)
    private String name;

    @ManyToOne
    @JoinColumn(name = "subentity1_id")
    private Subentity1 subentity1;

    @JsonProperty
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JsonProperty
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty
    public Subentity1 getSubentity1() {
        return subentity1;
    }

    public void setSubentity1(Subentity1 subentity1) {
        this.subentity1 = subentity1;
    }

这是我的转换方法的代码:

 private String toJSON(Object model) {
        ObjectMapper mapper = new ObjectMapper();
        String result = "";
        try {
            result = mapper.writeValueAsString(model);
        } catch (JsonGenerationException e) {
            LOG.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
        return result;
    }

我将非常感谢任何帮助、建议或代码:)

更新

alsp,我忘了从我的控制器中添加一段代码:

String result = "";
        List<SomeEntity> entities = someEntityService.getAll();
        Hibernate.initialize(entities);
        for (SomeEntity someEntity : entities) {
            Hibernate.initialize(someEntity.mainEntity());
            Hibernate.initialize(someEntity.mainEntity().subentity1());
            Hibernate.initialize(someEntity.mainEntity().subentity2());
        }
        result = this.toJSON(entities);

我不能忽略任何字段,因为我需要它们

【问题讨论】:

  • 您使用的是 Jackson Hibernate 模块吗? (github.com/FasterXML/jackson-module-hibernate)
  • 不,因为我使用的是 jackson 1.x 版本
  • 好的。该模块还有一个早期版本(适用于 1.9)(在 Maven 存储库中有一个分支,1.9.0 版本),这是值得的。

标签: java json spring hibernate jackson


【解决方案1】:

基本上,您的一些字段被包装到惰性休眠代理中。 在序列化你的对象之前调用Hibernate.initialize(model),它会加载你的惰性集合和引用。

但我不会混合使用数据库和视图模型,这是一种不好的做法。为您的 restful 模型创建一组类,并在序列化之前将数据库实体转换为它们。

【讨论】:

  • 您是否尝试过@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 上一个问题的建议?
  • 实际问题是我需要所有这些字段
  • 为什么需要代理字段?
  • 我认为这是一个相当无用的问题 :) 我需要它们,它们是主要实体的一部分,我需要它们在客户端进行操作
  • handler 不是你的模型的一部分,它是Javassist代理添加的一个字段,你不需要它可以忽略
【解决方案2】:

我创建了一个带有平面字段(String、Boolean、Double 等)的 Bean,它们在我的类中,并为转换提供了方法

【讨论】:

    【解决方案3】:

    如果你使用延迟加载添加这个

    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多