【问题标题】:Spring-data-rest testing in POSTMANPOSTMAN 中的 Spring-data-rest 测试
【发布时间】:2015-04-20 07:16:41
【问题描述】:

我正在 Spring-data-rest 中开发一个应用程序。我正在测试来自 POSTMAN 客户端的 POST 请求,以测试数据是否插入数据库。在我的数据库中,我有一个 cartItems 表。当我按如下方式发布 JSON 时,我能够发布数据:商品、购物车和商品类型是外键引用。

 {
"rate": 500,
"quantity": 1,
"amount": 500,
"createdAt": "2015-04-12T23:40:00.000+0000",
"updatedAt": "2015-04-14T21:35:20.000+0000",
"merchandise": "http://localhost:8080/sportsrest/merchandises/10",
"cart":"http://localhost:8080/sportsrest/carts/902",
"merchandiseType":"http://localhost:8080/sportsrest/merchandiseTypes/1"
  }

但是,当我按如下方式发布数据时,我收到了错误:我放置了商品 JSON,而不是商品的 URL,我已经测试了商品表 JSON 的 POST 请求:

 {
"rate": 500,
"quantity": 1,
"amount": 500,
"createdAt": "2015-04-12T23:40:00.000+0000",
"updatedAt": "2015-04-14T21:35:20.000+0000",
"merchandise": {
   "id": 4,
   "shortDescription": "white football",
   "rate": 500,
   "updatedAt": "2015-04-24T18:30:00.000+0000",
   "createdAt": "2015-04-20T18:30:00.000+0000",
   "longDescription": "test description for binary 1001",
   "type": "1"
    },
"cart":"http://localhost:8080/sportsrest/carts/902",
"merchandiseType":"http://localhost:8080/sportsrest/merchandiseTypes/1"

}

我收到以下错误:

 {
"cause": {
"cause": {
"cause": null,
"message": "Template must not be null or empty!"
  },
"message": "Template must not be null or empty! (through reference chain:   co.vitti.sports.bean.CartItem[\"merchandise\"])"
 },
 "message": "Could not read JSON: Template must not be null or empty! (through reference chain: co.vitti.sports.bean.CartItem[\"merchandise\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: co.vitti.sports.bean.CartItem[\"merchandise\"])"
}

有人可以帮我解释为什么我会收到这个错误。 谢谢。

【问题讨论】:

    标签: json spring rest spring-data-rest


    【解决方案1】:

    我猜你没有提供 Content-Type: application/json 标头。

    【讨论】:

    • 感谢重播。我将 Content-Type 提供为 Application/json。我们可以像上面那样将引用表的链接发布为 JSON 吗?
    • 嘿,抱歉我的仓促回答。我只是没有仔细阅读你的问题。默认情况下,Spring Data Rest 将尝试找出发送的数据是什么。为此,它会考虑 URI。如果你真的需要使用后一个例子,你应该编写一个自定义的反序列化器。
    • 没关系。感谢您的回复。
    【解决方案2】:

    尝试使用“@RestResource(exported = false)”注释您的“MerchandiseRepository”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 2016-04-17
      • 2018-11-19
      • 2017-01-19
      • 2020-04-04
      • 2019-03-28
      • 2016-10-26
      • 1970-01-01
      相关资源
      最近更新 更多