Spring MVC와 Spring HATEOAS를 이용해 프로젝트를 진행 중이었다.

보다 좋은 REST 서버를 구성하기 위해 HATEOAS를 지원하려고, 추상화된 Controller에 link를 삽입 중이었다.

아래는 추상화를 진행하던 코드였다.

1
2
3
4
5
6
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ENTITY get(@PathVariable("id") KEY id) {
ENTITY entity = service.findOne(id);
entity.add(linkTo(methodOn(this.getClass()).get(id)).withSelfRel());
return entity;
}

그런데 [methodOn]:1을 사용하려는 데 아래와 같은 오류가 뜨며 Generic을 이용할 수가 없었다.

1
2
3
4
5
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [@org.springframework.web.bind.annotation.PathVariable ?] to type [java.lang.String]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
at org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor$BoundMethodParameter.asString(AnnotatedParametersParameterAccessor.java:172)
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:128)

methodOn을 통해 얻은 객체를 캐스팅도 해 보고 별 짓을 다 해봤는데 도무지 문제를 알 수가 없었다.

그런데 문제는 의외로 쉬운 곳에 있었다.

Generic에는 @RestController, @RequestMapping 등을 사용하면 안되는데, 여기도 마찬가지였던 것이다.

바로 @PathVariable("id")가 문제였던 것.

이 부분을 수정하자 잘 작동했다!

..라고 쓰고싶었으나 결국 실패. 아오 짜증…. 걍 때려치웠다.