DomainClassConverter 사용 관련해서 MethodArgumentConversionNotSupportedException 에러가 납니다.
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이하 버전을 쓰시기 바랍니다.
spring boot 2.7.13-SNAPSHOT trace 소문자 로그 안나옴
0
533
1
<스프링 데이터 Common: 기본 리포지토리 커스터마이징> 에 대한 질문
0
392
1
comment table에서 저장될떄 왜 id값이 2부터저장이되는건가요?
0
407
1
@EnableJpaRepositories 설정을 스프링부트가 어디에서 자동설정하나요?
0
450
0
PersistenceContext 관련 질문드립니다.
0
335
1
지금(Eager), 나중에(Lazy)의 의미를 모르겠습니다
0
338
1
transaction 구간이 길어질 경우의 처리방법 문의드립니다.
0
905
1
docker postgres
0
292
1
Multiple DataSource 사용 시 transaction 관련 질문 드립니다.
0
2908
1
entity 중 null이 아닌 필드만 update 할 방법이 있을까요?
0
1190
1
Eager 모드일 경우, join을 inner join으로 바꾸는 법이 있을까요?
0
385
1
엔티티를 상속받는 DTO가 일반적인가요?
1
1847
1
커스텀 타입 클래스를 String 타입 처럼 이용해 쿼리하는 방법에 대해 질문하고 싶습니다.
0
339
1
연관관계 매핑 어떤식으로 해야될지 감이 안잡힙니다.
0
566
4
EntityManager 주입시 Annotation관련 질문드립니다.
0
565
1
클래스 기반 프로젝션 사용 관련 질문
0
560
1
save 메서드 질문드립니다.
0
258
1
복잡한 통계쿼리도 JPA로 가능한가요?
2
5592
1
find 와 get의 차이가 무엇인가요?
0
890
1
실무에서 JPA 할 때 FK로 개발할때 연관관계를 꼭 맺어주어야 하나요?
0
998
1
\dt Did not find any relations.
0
481
1
소스코드는 어디서 볼 수 있을까요?
1
287
1
table 생성과 select 문에 대한 질문
0
174
1
스프링 데이터 RepositoryTest 관련 질문
0
2173
2





