我正在尝试使用snippet实现ascii doc for grails rest-api,并为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$1.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
请纠正我出错的地方......
答案 0 :(得分:1)
我发现此失败的唯一原因是您没有从/api/v1/user
端点获得预期的响应。
请按照以下步骤操作:
您的代码和JSON看起来很好。
另外,请确保遵循此issue并将空数组字段标记为可选,并明确为其内容提供类型。
我希望这会有所帮助。
答案 1 :(得分:1)
我经常看到错误: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文件中设置了一些测试数据。
如果我能提供帮助,请告诉我。