【问题标题】:retrieving vavr collection via spring data rest from mongo db doesn't work通过来自 mongo db 的 spring data rest 检索 vavr 集合不起作用
【发布时间】:2018-03-18 07:34:56
【问题描述】:

我创建了一个小型 Spring Boot 项目,以通过包含 Seq(vavr 集合)的 spring data rest 从 mongoDb 检索示例对象。启动应用程序后立即不起作用。起初我必须进行插入,然后它可以调用 repo 的 rest 端点。

来自日志的错误消息

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

如果能提示缺少什么,那就太好了。

一些细节:

  • 最新的 Spring Boot 里程碑:2.0.0.M3
  • 我在 ObjectMapper 注册了 VavrModule
  • @EnableMongoRepositories(basePackages = "我的课程路径")

【问题讨论】:

  • 我在使用 spring boot 1.5.5.RELEASE 时遇到了同样的行为

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


【解决方案1】:

我更新你的项目

  • 弹簧靴2.0.0.RELEASE
  • 嵌入式Mongo 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
  }
}

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多