【问题标题】:Direct self-reference leading to cycle (through reference chain: java.util.ArrayList[0]-直接自引用导致循环(通过引用链:java.util.ArrayList[0]-
【发布时间】:2018-01-21 21:11:46
【问题描述】:

当我要向我的 api 发出请求时,我得到了这个错误: 我试图找到一些关于它的问题,但没有找到任何可以适用于我的问题的问题和答案,我希望你们中的一个可以为我做到这一点。

   com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: java.util.ArrayList[0]->digifred.arrecadacao.producaoPrimaria.model.Produtos["produtoPrincipal"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:269) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:879) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:666) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.7.4.jar:2.7.4]

我在前端使用 Spring boot aplication 和 angularJs .. 看看我的代码:

我对 api 的请求:

carregarProdutos = function() {
        $http({
            method : 'GET',
            url : '/user/produtos'              
        }).then(function(response) {
            $scope.produtos = response.data;
            $scope.idEntidade = response.data[0].entidade.idEntidade;
            $scope.nomeEntidade = response.data[0].entidade.nome;

        }, function(response) {
            console.log(response.data);
            console.log(response.status);
        });
    }; 

我的控制器

@RequestMapping(method = RequestMethod.GET, value = "/produtos")
    public ResponseEntity<Collection<Produtos>> buscarTodosProdutos(HttpServletRequest request) throws  Exception { 

        String idEntidadeCrypt = request.getHeader("DataBase");
        Long idEntidade = Long.parseLong(Crypto.decode(idEntidadeCrypt));

        Collection<Produtos> buscados = proService.buscarFiltro(idEntidade);
            return new ResponseEntity<>(buscados, HttpStatus.OK);
    } 

和我的班级模型:

@Entity
@Audited
@AuditTable(schema = "aud", value = "arr_produtos")
@EntityListeners(AuditingEntityListener.class)
@Table(name = "produtos", schema = "arr")
public class Produtos implements Serializable {


    private static final long serialVersionUID = 4211612266058833945L;


    @NotNull
    @JoinColumn(name = "id_entidade")
    @ManyToOne(/*cascade = CascadeType.ALL*/)
    private Entidades entidade;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_produto")
    private Long idProduto;


    @Basic(optional = false)
    @Column(name = "codigo_chave")
    private String codigoChave;



    @Column(name = "abreviatura")
    private String abreviatura;



    @JoinColumn(name = "id_produto_principal")
    @ManyToOne
    private  Produtos produtoPrincipal;

【问题讨论】:

    标签: java spring-boot spring-data spring-data-jpa jackson-databind


    【解决方案1】:

    错误的哪一部分让您感到困惑? produtoPrincipal 字段引用对象本身,即x.produtoPrincipal = x

    当您导出为 JSON 时,它会变成一个无休止的 JSON:

    {
      "idProduto": 1,
      "produtoPrincipal": {
        "idProduto": 1,
        "produtoPrincipal": {
          "idProduto": 1,
          "produtoPrincipal": {
            "idProduto": 1,
            "produtoPrincipal": ...
    

    要么:

    1. 修复数据,使其不引用自身。

    2. 找出你想要的 JSON 是什么,然后相应地更改代码,例如不要为“主体”包含完整的 Produtos 对象,只包含标识符:

      {
        "idProduto": 1,
        "idProdutoPrincipal": 1
      }
      

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2021-08-28
      • 2017-07-27
      • 1970-01-01
      相关资源
      最近更新 更多