작업물 공유, 학습 내용 공유 :hammer:
정은님 : spring rest docs 적용
(바빠서 API 및 Custom은 만들지 못함,)
rest docs 공유 by 정은
[toc]
참고
참고-우아한형제들
기본 문법: https://narusas.github.io/2018/03/21/Asciidoc-basic.html
실행
// 환경변수
$ export dbkey=test
// build
$ ./gradlew clean build -Dspring.profiles.action=local --info
// jar파일 실행, 경로: /build/libs (develop)
$ java -jar springboot-webatoz-0.0.1.jar
:red_circle: ISSUE
한글깨짐 Encoding설정 해야함
Rest Docs
Spring Rest Docs vs Swagger
| - | Spring Rest Docs | Swagger | | —- | ——————————————— | —————————————— | | 장점 | 코드 영향 X | API테스트 화면 제공 | | | 테스트 케이스가 성공해야 문서 조각이 만들어짐 | 적용이 쉽다 | | | | | | 단점 | 적용이 어려움 | 코드에 영향(어노테이션 추가 및 Config설정) | | | | 코드와 동기화가 안될수도 | Markdown으로 작성이 가능하지만 AsciiDoc이 더 편리해서 AsciiDoc적용
Markdown적용참고
적용
commit build.gradle
// 플러그인 plugins { id "org.asciidoctor.convert" version "1.5.9.2" } // 문서조각 위치 ext { set('snippetsDir', file("build/generated-snippets")) } //asciidoctor 설정 asciidoctor { inputs.dir snippetsDir dependsOn test } // .jar파일 생성 bootJar { dependsOn asciidoctor // gradle build시 ./build/asciidoc/html5/에 *.html파일 생성 // 피 파일이 .jar 안에 /static/docs/ 폴더에 복사가 됨 from ("${asciidoctor.outputDir}/html5") { into 'static/docs' } } dependencies { // 라이브러리 다운로드 asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor:2.0.3.RELEASE' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' }
Base Test Controller
// uri 설정 -> 기본 설정값이 http://localhost:8080인데 우리 host에 맞춰서 문서에 나타남 @AutoConfigureRestDocs(uriScheme = "https", uriHost = "webatoz.com", uriPort = 443) // rest docs confing 설정 파일 import @Import(RestDocsConfiguration.class)
@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc @AutoConfigureRestDocs(uriScheme = "https", uriHost = "webatoz.com", uriPort = 443) @Import(RestDocsConfiguration.class) public class BaseControllerTest { @Autowired protected MockMvc mockMvc; @Autowired protected ObjectMapper objectMapper; }
RestDocsConfiguration -> 여기서 인코딩 설정 해야할거같음 -> 일단 prettyPrint만 설정
@TestConfiguration public class RestDocsConfiguration { @Bean public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer() { return configurer -> configurer.operationPreprocessors() .withRequestDefaults(prettyPrint()) .withResponseDefaults(prettyPrint()); } }
ControllerTest
.optional()
requestHeaders( headerWithName("Content-Type").description("application/json"), headerWithName("Accept").description("application/hal+json") ), requestParameters( parameterWithName("page").description("The page to retrieve (default=1)").optional(), parameterWithName("size").description("Entries per page (default=20)").optional() ), links( linkWithRel("self").description("link to self"), linkWithRel("profile").description("document link"), linkWithRel("first").description("The first page of results").optional(), linkWithRel("prev").description("The previous page of results").optional(), linkWithRel("next").description("The next page of results").optional(), linkWithRel("last").description("The last page of results").optional() ), relaxedResponseFields( fieldWithPath("_embedded.boards").description("list of board"), fieldWithPath("_embedded.boards[].id").description("id"), fieldWithPath("_embedded.boards[].title").description("title"), fieldWithPath("_embedded.boards[].created_date").description("created_date").optional(), fieldWithPath("_embedded.boards[].updated_date").description("updated_date").optional(), fieldWithPath("_embedded.boards[].deleted_date").description("deleted_date").optional(), fieldWithPath("page.size").description("elements size per page"), fieldWithPath("page.total_elements").description("total element count of results"), fieldWithPath("page.total_pages").description("total page count"), fieldWithPath("page.number").description("current page")
다음 주 예정
:stars: 다들 Issue에 일정 comment 남겨주세요 !
문상수 -> 지도API 사용법 공유 예정
김성모 -> 식당 데이터 리스트 화면 실제 api 통신 데이터 넣기 + 성혜님과 이미지 관련 추가 작업
임수빈 -> Security 적용 완료, JWT 적용 후 리뷰 예정