EventController
@Controller @RequestMapping(value = "/api/events", produces = MediaTypes.HAL_JSON_VALUE) @RequiredArgsConstructor @Log4j2 public class EventController { @PostMapping public ResponseEntity createEvent(@RequestBody @Valid EventDto eventDto, Errors errors){ if(errors.hasErrors()){ return ResponseEntity.badRequest().body(errors); } //커스텀 validate 검사 eventValidator.validate(eventDto, errors); if(errors.hasErrors()){ return ResponseEntity.badRequest().body(errors); } //modelMapper 오류 //Event event=modelMapper.map(eventDto, Event.class); Event event = eventDto.toEvent(); Integer eventId = event.getId(); //유료인지 무료인지 변경처리 event.update(); Event newEvent=this.eventRepository.save(event);//저장 /** * * ★ 링크 생성하기 * EntityModel.of(newEvent); Resource 객체를 가져와서 사용 * * **/ WebMvcLinkBuilder selfLinkBuilder = linkTo(EventController.class).slash(eventId); URI createdUri = selfLinkBuilder.toUri(); EntityModel eventResource = EntityModel.of(newEvent); eventResource.add(linkTo(EventController.class).slash(eventId).withSelfRel()); eventResource.add(linkTo(EventController.class).withRel("query-events")); eventResource.add(selfLinkBuilder.withRel("update-event")); return ResponseEntity.created(createdUri).body(eventResource); }
EventControllerTests
@SpringBootTest @AutoConfigureMockMvc public class EventControllerTests { @Autowired MockMvc mockMvc; @Autowired ObjectMapper objectMapper; @Test @DisplayName("정상적으로 이벤트를 생성하는 테스트") public void createEvent() throws Exception{ Event event =Event.builder() .id(100) .name("Spring") .description("REST API Development with Spring") .beginEnrollmentDateTime(LocalDateTime.of(2023, 05, 06, 19 , 20 )) .closeEnrollmentDateTime(LocalDateTime.of(2023, 05, 20, 20 , 20)) .beginEventDateTime(LocalDateTime.of(2023, 05, 20, 20, 20)) .endEventDateTime(LocalDateTime.of(2023, 11, 26, 20, 20)) .basePrice(100) .maxPrice(200) .limitOfEnrollment(100) .location("강남역 D2 스타텀 팩토리") .free(false) .offline(false) .eventStatus(EventStatus.DRAFT) .build(); mockMvc.perform( post("/api/events") .contentType(MediaType.APPLICATION_JSON_UTF8) .accept(MediaTypes.HAL_JSON) .content(objectMapper.writeValueAsString(event)) ) .andDo(print()) .andExpect(status().isCreated()) .andExpect(jsonPath("id").exists()) .andExpect(header().exists(HttpHeaders.LOCATION)) .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE)) //.andExpect(header().string(HttpHeaders.CONTENT_TYPE, "application/hal+json;charset=UTF-8")) .andExpect(jsonPath("free").value(false)) .andExpect(jsonPath("offline").value(true)) // .andExpect(jsonPath("eventStatus").value(EventStatus.DRAFT.toString())) .andExpect(jsonPath("_links.self").exists()) .andExpect(jsonPath("_links.query-events").exists()) .andExpect(jsonPath("_links.update-event").exists()); }
다음과 같이 한글 깨짐 출력이 나타나는데
{ "id":1, "name":"Spring", "description":"REST API Development with Spring", "beginEnrollmentDateTime":"2023-05-06T19:20:00", "closeEnrollmentDateTime":"2023-05-20T20:20:00", "beginEventDateTime":"2023-05-20T20:20:00", "endEventDateTime":"2023-11-26T20:20:00", "location":"ê°ë¨ì D2 ì¤íí í©í 리", "basePrice":100, "maxPrice":200, "limitOfEnrollment":100, "offline":true, "free":false, "eventStatus":null, "_links":{ "self":{ "href":"http://localhost/api/events" }, "query-events":{ "href":"http://localhost/api/events" }, "update-event":{ "href":"http://localhost/api/events" } } }
다음을 참조해서 설정 변경
http://honeymon.io/tech/2019/10/23/spring-deprecated-media-type.html
그러나, 이해가 안되면 다음과 같이
application.properties에 설정 추가
# UTF-8 세팅 server.servlet.encoding.charset=UTF-8 server.servlet.encoding.force=true
테스트 설정
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, "application/hal+json;charset=UTF-8"))
{참 "id":1, "name":"Spring", "description":"REST API Development with Spring", "beginEnrollmentDateTime":"2023-05-06T19:20:00", "closeEnrollmentDateTime":"2023-05-20T20:20:00", "beginEventDateTime":"2023-05-20T20:20:00", "endEventDateTime":"2023-11-26T20:20:00", "location":"강남역 D2 스타텀 팩토리", "basePrice":100, "maxPrice":200, "limitOfEnrollment":100, "offline":true, "free":false, "eventStatus":null, "_links":{ "self":{ "href":"http://localhost/api/events" }, "query-events":{ "href":"http://localhost/api/events" }, "update-event":{ "href":"http://localhost/api/events" } } }
참조 :
https://smpark1020.tistory.com/169
댓글 ( 4)
댓글 남기기