I'm trying to call Spring's ControllerLinkBuilder.methodOn()
with a non-String type, which always fails. And I don't know which kind of Converter
to use and where to register it.
Here's my Controller:
@RestController
@RequestMapping("/companies")
class CompanyController {
@RequestMapping(value="/{c}", method=RequestMethod.GET)
void getIt(@PathVariable Company c) {
System.out.println(c);
Link link = linkTo(methodOn(getClass()).getIt(c));
}
}
The System.out.println(c)
works well. My Company
Domain object get's fetched from DB. (I'm using DomainClassConverter
)
But the other way doesn't work: ConverterNotFoundException: No converter found capable of converting from type @PathVariable Company to type String
Do I just need a Converter<Company, String>
? And where should I register it? I tried something within the addFormatters(FormatterRegistry registry)
method of WebMvcConfigurationSupport
, but it did just display the same error. But after all I'm not sure what exactly I tried...
I had the same issue, it is a bug. If you don't want to do copy & paste on every controller you can try something like this in your WebMvcConfigurationSupport
. It works for me.
@Override
public void addFormatters(final FormatterRegistry registry) {
super.addFormatters(registry);
try {
Class<?> clazz = Class.forName("org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor$BoundMethodParameter");
Field field = clazz.getDeclaredField("CONVERSION_SERVICE");
field.setAccessible(true);
DefaultFormattingConversionService service = (DefaultFormattingConversionService) field.get(null);
for (Converter<?, ?> converter : beanFactory.getBeansOfType(Converter.class).values()) {
service.addConverter(converter);
}
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With