강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của kmh8919
kmh8919

câu hỏi đã được viết

Phát triển API REST dựa trên mùa xuân

Tài liệu Spring REST Tạo các đoạn tài liệu khác nhau

두가지 추가적인 코드 공유드립니다.

Viết

·

368

9

1. rest docs 문서에 반환될 값들의 타입을 강하게 걸어서 확실한 테스트를 하고 싶으시다면 아래와 같이 type에 JsonFieldType을 추가하여 테스트할 수 있습니다.

2. 그리고 json의 특정 값이 리턴될 수 있고 안될 수 있는 결과라면.. 예를 들어, response에 description 필드가 있을 경우, 없을 경우를 하나의 문서로 만들기 위해서는 optional()을 붙여주면 됩니다. 결과적으로는  relaxed랑 비슷해졌지만.. 개개의 필드를 설정할 수 있다는 점이 강점인것 같네요.

responseFields(
fieldWithPath("id").type(JsonFieldType.NUMBER).description("event id"),
fieldWithPath("name").type(JsonFieldType.STRING).description("Name of new event"),
fieldWithPath("description").type(JsonFieldType.STRING).description("event description"),
fieldWithPath("beginEnrollmentDateTime").type(JsonFieldType.STRING).description("event enroll start date"),
fieldWithPath("closeEnrollmentDateTime").type(JsonFieldType.STRING).description("devnet enroll end date"),
fieldWithPath("beginEventDateTime").type(JsonFieldType.STRING).description("event start date"),
fieldWithPath("endEventDateTime").type(JsonFieldType.STRING).description("event end date"),
fieldWithPath("location").type(JsonFieldType.STRING).description("event location"),
fieldWithPath("basePrice").type(JsonFieldType.NUMBER).description("event basePrice"),
fieldWithPath("maxPrice").type(JsonFieldType.NUMBER).description("event maxPrice"),
fieldWithPath("limitOfEnrollment").type(JsonFieldType.NUMBER).description("limit enrollment"),
fieldWithPath("free").type(JsonFieldType.BOOLEAN).description("the event is free"),
fieldWithPath("offline").type(JsonFieldType.BOOLEAN).description("the event is offline"),
fieldWithPath("eventStatus").type(JsonFieldType.STRING).description("event status"),

//optional fields
fieldWithPath("_links.self.href").type(JsonFieldType.STRING).description("my href").optional(),
fieldWithPath("_links.query-events.href").type(JsonFieldType.STRING).description("my href").optional(),
fieldWithPath("_links.update-event.href").type(JsonFieldType.STRING).description("my href").optional()
)
javaspringrest-api

Câu trả lời 2

2

whiteship님의 프로필 이미지
whiteship
Người chia sẻ kiến thức

좋은 내용 공유 감사합니다.

0

공유 감사합니다 ~

 

Hình ảnh hồ sơ của kmh8919
kmh8919

câu hỏi đã được viết

Đặt câu hỏi