• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

DomainClassConverter 사용 관련해서 MethodArgumentConversionNotSupportedException 에러가 납니다.

20.06.18 18:44 작성 조회수 295

1

27강 DomainClassConverter  관련 수업을 들으며 코드 동일하게 따라치며 강의 듣고 있는데요, DomainClassConverter 사용 전 @PathVariable 을 사용하여 Long 으로 id 를 받아와서 repo 로 부터 엔티티를 꺼내 리턴하는 방식으로 동일한 테스트 코드에서 이상 없이 돌아갑니다.

하지만 DomainClassConverter 를 사용하는 취지로 @PathVariable("id") Post post 와 같이 작성한 뒤 테스트를 돌려보면 테스트가 돌아가지 않습니다. 구체적인 코드는 아래와 같습니다.

DomainClassConverter 사용 전 코드입니다.

@GetMapping("/posts/{id}")
  public String getPost(@PathVariable Long id) {
    Optional<Post> byId = postRepository.findById(id);
    Post post = byId.get();
    return post.getTitle();
  }

DomainClassConverter 기능을 사용하기 위해 변경한 코드입니다.

@GetMapping("/posts/{id}")
  public String getPost(@PathVariable("id") Post post) {
    return post.getTitle();
  }

테스트 코드 입니다.

@Test
  public void getPost() throws Exception {
    // Arrange
    Post post = new Post();
    post.setTitle("jpa");
    postRepository.save(post);

    // Act
    ResultActions actual = mockMvc.perform(get("/posts/" + post.getId())).andDo(print());

    // Assert
    actual.andExpect(status().isOk());
    actual.andExpect(content().string("jpa"));
  }

발생하는 예외 로그 입니다.

Hibernate: 

    call next value for hibernate_sequence

Hibernate: 

    insert 

    into

        post

        (created, title, id) 

    values

        (?, ?, ?)

2020-06-18 18:30:50.714 TRACE 86203 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [null]

2020-06-18 18:30:50.715 TRACE 86203 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [jpa]

2020-06-18 18:30:50.715 TRACE 86203 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BIGINT] - [1]

2020-06-18 18:30:50.752  WARN 86203 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.github.callmewaggs.commonweb.post.Post'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.github.callmewaggs.commonweb.post.Post': no matching editors or conversion strategy found]



MockHttpServletRequest:

      HTTP Method = GET

      Request URI = /posts/1

       Parameters = {}

          Headers = []

             Body = null

    Session Attrs = {}



Handler:

             Type = com.github.callmewaggs.commonweb.post.PostController

           Method = com.github.callmewaggs.commonweb.post.PostController#getPost(Post)



Async:

    Async started = false

     Async result = null



Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException



ModelAndView:

        View name = null

             View = null

            Model = null



FlashMap:

       Attributes = null



MockHttpServletResponse:

           Status = 500

    Error message = null

          Headers = []

     Content type = null

             Body = 

    Forwarded URL = null

   Redirected URL = null

          Cookies = []



MockHttpServletRequest:

      HTTP Method = GET

      Request URI = /posts/1

       Parameters = {}

          Headers = []

             Body = null

    Session Attrs = {}



Handler:

             Type = com.github.callmewaggs.commonweb.post.PostController

           Method = com.github.callmewaggs.commonweb.post.PostController#getPost(Post)



Async:

    Async started = false

     Async result = null



Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException



ModelAndView:

        View name = null

             View = null

            Model = null



FlashMap:

       Attributes = null



MockHttpServletResponse:

           Status = 500

    Error message = null

          Headers = []

     Content type = null

             Body = 

    Forwarded URL = null

   Redirected URL = null

          Cookies = []







java.lang.AssertionError: Status 

Expected :200

Actual   :500

<Click to see difference>

...생략

단순히 제가 오타가 있다거나 하는 문제인가요? 검색을 해봤는데도 해결책을 찾기 어려워서 질문 올립니다.

답변 1

답변을 작성해보세요.

0

재현이 되길래 좀 더 찾아봤는데요.
https://jira.spring.io/browse/DATACMNS-1734

스프링 데이터  2.3.1부터 생긴 변화인데.. 제가 볼때는 버그 같습니다.

일다은 스프링 부트 2.3.1 부터 스프링 데이터 2.3.1을 쓰기 시작하는거 같은데요. 버그가 제대로 리포팅이 되고 고쳐지기 전까지는 우선 스프링 부트 2.3.0이하 버전을 쓰시기 바랍니다.