Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- rest api
- 백준
- JPA
- @ConfigurationProperties
- 알고리즘
- Application Event
- webjar
- Application Argument
- Application Runner
- HATEOAS
- 백기선
- OAuth2
- Spring Security
- JsonSerializer
- 스프링 부트
- 리소스핸들러
- AuthenticationPrincipal
- 정적 리소스
- HttpMessageConverters
- 브루트포스
- application.properties
- 리소스 서버
- @Profile
- cors
- 다익스트라
- 스프링부트
- EnableAutoConfiguration
- 외부설정
- WebApplication Type
- 백트래킹
Archives
- Today
- Total
아카이브
[스프링 기반 REST API 개발] Event 생성 API, 테스트 코드 본문
ModelMapper 의존성 추가
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.9</version>
</dependency>
EventDto 추가
package me.jumen.demoinflearnrestapi.events;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class EventDto {
private Integer id;
private String name;
private String description;
private LocalDateTime beginEnrollmentDateTime;
private LocalDateTime closeEnrollmentDateTime;
private LocalDateTime beginEventDateTime;
private LocalDateTime endEventDateTime;
private String location; // (optional) 이게 없으면 온라인 모임
private int basePrice; // (optional)
private int maxPrice; // (optional)
private int limitOfEnrollment;
}
EventController
@PostMapping
public ResponseEntity createEvent(@RequestBody EventDto eventDto) {
Event event = modelMapper.map(eventDto, Event.class);
Event newEvent = this.eventRepository.save(event);
URI createdUri = linkTo(EventController.class).slash(newEvent.getId()).toUri();
return ResponseEntity.created(createdUri).body(newEvent);
}
modelMapper를 통해 EventDto -> Event 변환
Dto와 modelMapper를 통해 입력값을 제한할 수 있다.
HATEOAS가 제공하는 linkTo(), methodOn() 사용해서 Location URI 만들기
linkTo()로 생성된 uri : http://localhost:8080/api/events/1
ResponseEntity에 넣어주면, Http Header에 Location = http://localhost:8080/api/events/1로 들어간다.
@Test
public void create() throws Exception {
EventDto eventDto = EventDto.builder()
.name("안녕 이벤트")
.description("배고프다")
.beginEnrollmentDateTime(LocalDateTime.of(2018, 11, 2, 8, 0))
.closeEnrollmentDateTime(LocalDateTime.of(2018, 11, 3, 8, 0))
.beginEventDateTime(LocalDateTime.of(2018, 11, 4, 8, 0))
.endEventDateTime(LocalDateTime.of(2018, 11, 5, 8, 0))
.basePrice(0)
.maxPrice(0)
.location("네이버 D2 팩토리 좁았음")
.limitOfEnrollment(100)
.build();
this.mockMvc.perform(post("/api/events")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(header().exists("Location"))
.andExpect(jsonPath("free").value(false))
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("eventStatus").value(EventStatus.DRAFT.name()));
}
'Spring > 스프링 기반 REST API 개발' 카테고리의 다른 글
[스프링 기반 REST API 개발] Spring HATEOAS 적용 (0) | 2021.01.06 |
---|---|
[스프링 기반 REST API 개발] 입력 받을 수 없는 값의 경우 Bad Request (0) | 2021.01.05 |
[스프링 기반 REST API 개발] 테스트코드 리팩토링 - 파라미터 테스트 (junit-jupiter-params) (0) | 2021.01.05 |
[스프링 기반 REST API 개발] Bad Request 응답 본문 (Errors, JsonSerializer & @JsonComponent, BindingError&Validator ) (0) | 2021.01.05 |
[스프링 기반 REST API 개발] Event 도메인 구현 (0) | 2021.01.05 |