• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

안녕하세요 강사님. ObjectMapper가 @Autowired 못읽어오는데 어디서 잘못된건지 모르겠습니다.

21.10.30 02:51 작성 조회수 1.23k

0

안녕하세요. 강의 매우 잘 듣고 있습니다 !

다름이 아니라 @WebMvcTest 을 빼고 @AutoConfigureMockMvc 를 사용했을 떄, 

@Autowired ObjectMapper objectMapper;

이 쪽에서 에러가 발생하는데 어디쪽을 보면 될까요..?

Could not autowire. No beans of 'ObjectMapper' type found.



감사합니다.

답변 1

답변을 작성해보세요.

2

안녕하세요.

@SpringBootTest는 추가하셨나요?

@WebMvcTest를 안쓰실꺼라면 @SpringBootTest와 @AutoConfigureMockMvc를 조합해서 쓰셔야 합니다.

감사합니다.

조재연님의 프로필

조재연

질문자

2021.11.02

안녕하세요.

네 맞습니다. 조합해서 사용했는데 에러가 발생하네요.

그래서 스택 오버플로우 보는데 

https://stackoverflow.com/questions/30060006/how-do-i-obtain-the-jackson-objectmapper-in-use-by-spring-4-1

 

이거 참고하라고 해서  이런식으로 했는데도,

Could not autowire. No beans of 'Jackson2ObjectMapperBuilder' type found. 

이렇게 에러가 발생하네요.

 

감사합니다.

// 처음 코드
@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
class EventControllerTest {

@Autowired MockMvc mockMvc;
@Autowired ObjectMapper objectMapper;

@Test
void createEVent() throws Exception {
Event event = Event.builder()
.id(100)
.name("Spring")
.description("REST API Development with Spring")
.beginEventDateTime(LocalDateTime.of(2021, 10, 30, 12, 00))
.closeEnrollmentDateTime(LocalDateTime.of(2021, 10, 31, 12, 00))
.beginEnrollmentDateTime(LocalDateTime.of(2021, 11, 01, 12, 00))
.endEventDateTime(LocalDateTime.of(2021, 11, 02, 12, 00))
.basePrice(100)
.maxPrice(200)
.limitOfEnrollment(100)
.location("강남역")
.free(true)
.offline(false)
.build();

mockMvc.perform(post("/api/event")
.contentType(MediaType.APPLICATION_JSON)
.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(jsonPath("id").value(Matchers.not(100)))
.andExpect(jsonPath("free").value(Matchers.not(true)));
}
}



// stackoverflow 수정 코드
@Autowired Jackson2ObjectMapperBuilder builder;

@Test
void createEVent() throws Exception {
ObjectMapper objectMapper = builder.build();

에러가 재현되는 프로젝트를 깃헙에 올려서 공유해 주시면 내려 받아서 확인해 보겠습니다.

조재연님의 프로필

조재연

질문자

2021.11.03

https://github.com/JaeYeon33/study <- 여기에 올려두었습니다. 감사합니다 !

조재연님의 프로필

조재연

질문자

2021.11.21

안녕하세요. 강사님

해결을 했습니다.

제가 경로를 이상하게 만들어뒀네요... ㅜ

이런 실수를 하다니..  부끄럽네요

나머지 강의도 잘 듣겠습니다!