Grails 3.1.8:Spring 使用 Rest Assured 的 Rest 文档给出 PayloadHandlingException:com.fasterxml.jackson.databind.JsonMappingException

Grails 3.1.8 : Spring Rest Docs using Rest Assured giving PayloadHandlingException: com.fasterxml.jackson.databind.JsonMappingException

我正在尝试使用 grails rest-api 片段实现 ascii 文档,并确保 json 响应:

    {
  "instanceList": [
    {
      "firstName": "Coy",
      "lastName": "T",
      "pictureUrl": null,
      "email": "bootstrap@cc.com",
      "bio": null,
      "skills": [],
      "interestAreas": []
    },
    {
      "firstName": "Jane",
      "lastName": "D",
      "pictureUrl": null,
      "email": "jane@cc.com",
      "bio": null,
      "skills": [],
      "interestAreas": []
    },
    {
      "firstName": "Cause",
      "lastName": "C",
      "pictureUrl": "https://cc-picture.com",
      "email": "cc-user@code.com",
      "bio": "cc beyond infinity",
      "skills": [],
      "interestAreas": []
    },
    {
      "firstName": "sachidanand",
      "lastName": "v",
      "pictureUrl": null,
      "email": "cc@cc.com",
      "bio": null,
      "skills": [],
      "interestAreas": []
    }
  ],
  "totalCount": 4
}

和 UserDocumentationApiSpec 的代码片段(作为 IntegrationTest):

void 'test and document get request for /user'() {
        expect:
        given(documentationSpec)
                .header("AuthToken", "TokenValue")
                .accept(MediaType.APPLICATION_JSON.toString())
                .filter(document('user-list-v1',
                preprocessRequest(modifyUris()
                        .host('127.0.0.1')
                        .removePort()),
                preprocessResponse(prettyPrint()),
                responseFields(
                        fieldWithPath("[].firstName").description("First name of user"),
                        fieldWithPath("[].lastName").description("Last name of user"),
                        fieldWithPath("[].pictureUrl").type(JsonFieldType.STRING).description("Picture Url of user"),
                        fieldWithPath("[].email").description("Email address of user"),
                        fieldWithPath("[].bio").description("Bio data of user"),
                        fieldWithPath("totalCount").description("Count of instanceList field"),
                        fieldWithPath("type").description("Type of result")
                ))).
                when()
                .port(8080)
                .get('/api/v1/user')
                .then()
                .assertThat()
                .statusCode(is(200))
    }

这部分代码给我的错误跟踪为:

org.springframework.restdocs.payload.PayloadHandlingException: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: [B@2c6adbe3; line: 1, column: 1]
    at org.springframework.restdocs.payload.JsonContentHandler.readContent(JsonContentHandler.java:84)
    at org.springframework.restdocs.payload.JsonContentHandler.findMissingFields(JsonContentHandler.java:50)
    at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:113)
    at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:74)
    at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
    at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:192)
    at org.springframework.restdocs.restassured.RestDocumentationFilter.filter(RestDocumentationFilter.java:63)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
    at com.jayway.restassured.filter.session.SessionFilter.filter(SessionFilter.java:60)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
    at org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer.filter(RestAssuredRestDocumentationConfigurer.java:65)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1574)
    at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:159)
    at com.converge.docs.UserApiDocumentationSpec.$tt__$spock_feature_0_0(UserApiDocumentationSpec.groovy:73)
    at com.converge.docs.UserApiDocumentationSpec.test and document get request for /user_closure2(UserApiDocumentationSpec.groovy)
    at groovy.lang.Closure.call(Closure.java:426)
    at groovy.lang.Closure.call(Closure.java:442)
    at grails.transaction.GrailsTransactionTemplate.doInTransaction(GrailsTransactionTemplate.groovy:70)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
    at com.converge.docs.UserApiDocumentationSpec.test and document get request for /user(UserApiDocumentationSpec.groovy)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: [B@2c6adbe3; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3781)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3721)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2819)
    at org.springframework.restdocs.payload.JsonContentHandler.readContent(JsonContentHandler.java:81)
    ... 21 more

不对的地方请指正...

我看到此失败的唯一原因是您没有从 /api/v1/user 端点获得预期的响应。

按照以下步骤操作:

  1. 检查数据库中是否存在实例。
  2. 检查您发送的令牌是否正确。
  3. 使用 RestBuilder 编写一个测试用例,看看您是否真的得到了预期的响应。

您的代码和 JSON 看起来不错。

此外,请务必遵循此 issue 并将空数组字段标记为可选,并明确为其内容提供类型。

希望对您有所帮助。

我经常看到错误:org.springframework.restdocs.payload.PayloadHandlingException: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

没有反应时。我看到这是一个 get 请求。这些集成测试在 Grails 中的工作方式与 RestClient 略有不同。您是否在此测试或 bootstrap.Groovy 文件中设置了示例数据点?我看不到其余代码,看看你怎么样 运行 这是一个集成测试。在我的示例 Grails 示例中,我在 Bootstrap.groovy 文件中设置了一些测试数据。

如果我能帮上忙,请告诉我。