일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 외부설정
- WebApplication Type
- @ConfigurationProperties
- JsonSerializer
- rest api
- 백준
- JPA
- Spring Security
- HATEOAS
- cors
- 리소스 서버
- AuthenticationPrincipal
- 리소스핸들러
- 스프링 부트
- 백트래킹
- Application Runner
- 백기선
- HttpMessageConverters
- @Profile
- 브루트포스
- webjar
- 알고리즘
- OAuth2
- Application Event
- application.properties
- Application Argument
- EnableAutoConfiguration
- 스프링부트
- 정적 리소스
- 다익스트라
- Today
- Total
목록Spring/스프링 기반 REST API 개발 (23)
아카이브
EventController에 getEvent 구현 @GetMapping("/{id}") public ResponseEntity getEvent(@PathVariable Integer id) { Optional byId = this.eventRepository.findById(id); if (byId.isEmpty()) { return ResponseEntity.notFound().build(); } Event event = byId.get(); /* HATEOAS links add */ EventResource eventResource = new EventResource(event); eventResource.add(new Link("/docs/index.html#resources-events-get"..
페이징, 정렬을 어떻게 할까? - spring-data-jpa가 제공하는 Pageable을 사용 Page에 안에 들어있는 Event 들은 리소스로 어떻게 변경할까? - 하나씩 순회하면서 직접 EventResource로 맵핑을 시킬까.. - PagedResourceAssembler 사용하기 queryEvents handler 추가하기 @GetMapping public ResponseEntity queryEvents(Pageable pageable, PagedResourcesAssembler assembler) { Page page = this.eventRepository.findAll(pageable); PagedModel entityModels = assembler.toModel(page, EventRe..
Index Handler는 다른 리소스에 대한 링크를 제공한다.(EventController 및 문서화) @RestController public class IndexController { @GetMapping("/api") public RepresentationModel index() { RepresentationModel index = new RepresentationModel(); index.add(linkTo(EventController.class).withRel("events")); return index; } } ErrorResource 작성 : 에러 발생 시 Index 링크 제공 public class ErrorsResource extends EntityModel { public Errors..
애플리케이션 설정과 테스트 설정 중복 어떻게 줄일 것인가? - @SpringBootTest 통합 테스트 시 모든 빈들을 등록하게 된다. 이때 Postgresql을 사용한다. - @WebMvcTest와 같은 Mock Test의 경우는 기본적으로 H2 인메모리 DB를 사용하고, mock bean들을 등록한다. - test/resources에 test용 application.properties를 생성하고 활용하면, properties 우선순위로 인해 main의 같은 파일을 overriding 해서 같은 설정이 아니라면 값이 상실되기에 중복 코드의 문제점이 존재한다. Profile과 @ActiveProfiles 로 중복 문제를 해결 - 테스트 클래스에 @ActiveProfiles("test") 기입하여, mai..
Mavn plugin 설정하기 org.asciidoctor asciidoctor-maven-plugin 1.5.8 generate-docs prepare-package process-asciidoc html book org.springframework.restdocs spring-restdocs-asciidoctor ${spring-restdocs.version} maven-resources-plugin copy-resources prepare-package copy-resources ${project.build.outputDirectory}/static/docs ${project.build.directory}/generated-docs adoc template 추가 src/main/asciidoc/in..

API 문서 만들기 요청 본문 문서화: request-body 응답 본문 문서화: response-body 링크 문서화: links self query-events update-event profile 링크 추가 요청 헤더 문서화: requestHeaders 요청 필드 문서화: requestFields 응답 헤더 문서화: responseHeaders 응답 필드 문서화: responseFields links : linkWithResl() ~Headers : headerWithName ~Fields : fieldWithPath @Test @DisplayName("정상적으로 이벤트를 생성하는 테스트") void createEvent() throws Exception { EventDto eventDto = Ev..
Spring REST Docs란? API 문서 작성 자동화 도구 - 성공 Test Case를 기반으로 API 스펙을 작성하기 때문에, 제품 코드를 건드릴 필요가 없다 - 문서의 조각을 만드는 것을 snippets라고 칭한다. - API 코드를 변경했을 때, Test Case가 성공하면 문서도 자동으로 바뀐다. - 추가 테스트에 대한 문서화가 강제된다. Asciidoctor ? HTML 문서 작성 도구 - Rest Docs가 문서 작성에 필요한 코드 조각을 만드는 도구라면, Asciidoctor는 Adoc 파일을 활용하여 html 문서를 만들어주는 도구이다. 테스트를 통한 snippets 생성 방법 - MockMvc(@WebMvcTest) : Contoller Layer 만 테스트하기에 속도가 빠르다. -..

스프링 부트가 HATEOAS 자동설정을 지원하므로, @EnableEntityLinks, @EnableHypermediaSupport 어노테이션 X 1. extends ResourceSupport 문제 : objectMapper가 BeanSerializer로 serialize하는데, BeanSerializer는 기본적으로 그 객체의 이름으로 감싸서 보낸다. - @JsonUnwrapped 로 해결 : wrapping object를 unwrapping하여 json을 펼쳐준다. - extends EntityModel : 내부적으로 @JsonUnwrapped 처리가 되어있다. 2. Content-Type : application/hal+json 으로 보내면 client가 _li..
입력받을 수 없는 값들이 넘어왔을 경우 Bad Request를 응답한다. ObjectMapper 설정하기 - serialize : Object -> JSON -deSerialize : JSON -> Object JSON -> Object 변환 시 unknown properties(입력받을 수 없는 값) 일 경우 오류가 발생하는데, 이를 무시하기 위해 application-properties에 spring.jackson.deserialization.fail-on-unknown-properties=true를= 통해 무시한다. name, number를 받을 수 있는 경우에 size(unknown)을 넘길 경우, 이를 무시하고 deserialize를 진행한다. @DisplayName("입력받을 수 없는 값을 사..
기존의 테스트 코드 @Test void testFree() { // 1 Event event = Event.builder() .basePrice(0) .maxPrice(0) .build(); event.update(); assertThat(event.isFree()).isTrue(); // 2 event = Event.builder() .basePrice(1000) .maxPrice(0) .build(); event.update(); assertThat(event.isFree()).isFalse(); // 3 event = Event.builder() .basePrice(0) .maxPrice(1000) .build(); event.update(); assertThat(event.isFree()).isF..