0

我创建了一个小型 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 = "我的课程路径")
4

1 回答 1

1

我更新你的项目

  • 弹簧靴2.0.0.RELEASE
  • 嵌入式Mongode.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
  }
}
于 2018-03-14T13:23:08.177 回答