从mongo db通过spring数据休息检索vavr集合不起作用

时间:2017-10-06 13:18:34

标签: mongodb rest spring-boot spring-data-rest vavr

我创建了一个小的spring启动项目,通过包含Seq(vavr集合)的spring数据rest从mongoDb检索一个示例对象。启动应用程序后,它立即无效。首先,我必须进行插入,然后它可以调用teh repo的休息端点。

来自日志的错误消息

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Couldn't find PersistentEntity for type class io.vavr.collection.List$Cons!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Couldn't find PersistentEntity for type class io.vavr.collection.List$Cons! (through reference chain: org.springframework.hateoas.PagedResources["_embedded"]->java.util.Collections$UnmodifiableMap["myEntities"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->de.spring.demo.entity.MyEntity["myList"])

听起来像是必须注入一些映射器,这些映射器在插入新条目后自动注入(或者某些延迟加载可能......)

可在此处找到示例:https://github.com/renne-b/spring-rest-demo

如果能得到一个缺失的提示,那就太棒了。

一些细节:

  • 最新的春季启动里程碑:2.0.0.M3
  • 我在ObjectMapper
  • 注册了VavrModule
  • @EnableMongoRepositories(basePackages =“for my path to classes”)

1 个答案:

答案 0 :(得分:1)

我用

更新您的项目
  • Spring boot 2.0.0.RELEASE
  • EmbeddedMongo de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.0.3 / 3.2.2:Windows:B64
  • 修复JSON映射

在该请求curl http://localhost:8080/myEntities执行后没有问题:

{
  "_embedded" : {
    "myEntities" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/myEntities{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/myEntities"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }
}

添加2个实体后:

{
  "_embedded" : {
    "myEntities" : [ {
      "foo" : "bar1521033137701",
      "myList" : {
        "content" : [ "bla" ]
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/myEntities/5aa91fb1bec7c7169c9b5943"
        },
        "myEntity" : {
          "href" : "http://localhost:8080/myEntities/5aa91fb1bec7c7169c9b5943"
        }
      }
    }, {
      "foo" : "bar1521033145175",
      "myList" : {
        "content" : [ "bla" ]
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/myEntities/5aa91fb9bec7c7169c9b5944"
        },
        "myEntity" : {
          "href" : "http://localhost:8080/myEntities/5aa91fb9bec7c7169c9b5944"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/myEntities{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/myEntities"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}