如何为Spring RestDocs的嵌套订单对象(即MockMvc)指定FieldDescriptors

问题描述 投票:0回答:1

我有一个嵌套结构,要使用Spring MockMvc生成RestDoc。我在指定嵌套结构时遇到问题。到目前为止,这是我在不指定嵌套对象的情况下可以使用的功能。

Customer c = new Customer("John", 30);
Order order = new Order(....);
c.addOrder(order);

{
 "name":"John",
 "age":30,
 "orders": {
 "orderNumber":"12345",
  "quantity":"1",
  "productCode":"CDE-112",
  "productName" :"bicycle"     
}



 ResultActions resultActions = mockMvc.perform(get("/orders/user/{customerId}" ,

 "001022207")
             .contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(status().isOk()).andDo(document("getProductsForUser" ,
                    preprocessRequest(prettyPrint()) ,
                    preprocessResponse(prettyPrint()) ,
                    responseFields(
                            fieldWithPath("name").type(JsonFieldType.STRING).description("The customer name"),
                            fieldWithPath("age").type(JsonFieldType.NUMBER).description("The age of the customer.") ,

很遗憾,我不知道如何为客户添加嵌套订单。

如果有人可以帮助我弄清楚如何为嵌套顺序包括字段描述符,我将非常感激。

rest spring-mvc
1个回答
0
投票

这是我解决的方式:

ResultActions resultActions = mockMvc.perform(get(“ / orders / user / {customerId}”,

"001022207")
         .contentType(MediaType.APPLICATION_JSON_VALUE))
        .andExpect(status().isOk()).andDo(document("getProductsForUser" ,
                preprocessRequest(prettyPrint()) ,
                preprocessResponse(prettyPrint()) ,
                responseFields(

      fieldWithPath("name").type(JsonFieldType.STRING).description("The customer    name"),                            

fieldWithPath(“ age”)。type(JsonFieldType.NUMBER).description(“客户的年龄。”),fieldWithPath(“ orders。[]。orderNumber”)。description(“ Ordernumber”),fieldWithPath(“ orders。[]。quantity”)。description(“ quantityordered”)

等...

© www.soinside.com 2019 - 2024. All rights reserved.