问题描述
我正在尝试使用非字符串类型调用 Spring 的 ControllerLinkBuilder.methodOn()
,但总是失败.而且我不知道要使用哪种Converter
以及在哪里注册.
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.
这是我的控制器:
@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));
}
}
System.out.println(c)
运行良好.我的 Company
域对象从数据库中获取.(我正在使用 DomainClassConverter
)
The System.out.println(c)
works well. My Company
Domain object get's fetched from DB. (I'm using DomainClassConverter
)
但另一种方式不起作用:ConverterNotFoundException:找不到能够从@PathVariable Company 类型转换为String 类型的转换器
But the other way doesn't work: ConverterNotFoundException: No converter found capable of converting from type @PathVariable Company to type String
我是否只需要一个 Converter
?我应该在哪里注册?我在 WebMvcConfigurationSupport
的 addFormatters(FormatterRegistry registry)
方法中尝试了一些东西,但它确实显示了相同的错误.但毕竟我不确定我到底尝试了什么......
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...
推荐答案
找到了解决方案".它需要大量的复制&从 Spring 的类粘贴,但至少它有效!
Found a "solution". It requires a lot copy & paste from Spring's classes, but at least it works!
基本上我必须复制 org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor
并更改两行:
Basically I had to copy org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor
and change two lines:
class AnnotatedParametersParameterAccessor {
...
static class BoundMethodParameter {
// OLD: (with this one you can't call addConverter())
// private static final ConversionService CONVERSION_SERVICE = new DefaultFormattingConversionService();
// NEW:
private static final FormattingConversionService CONVERSION_SERVICE = new DefaultFormattingConversionService();
...
public BoundMethodParameter(MethodParameter parameter, Object value, AnnotationAttribute attribute) {
...
// ADD:
CONVERSION_SERVICE.addConverter(new MyNewConverter());
}
...
}
这个类被 ControllerLinkBuilderFactory
使用.所以我不得不复制 &也贴一下.
This class get's used by ControllerLinkBuilderFactory
. So I had to copy & paste that, too.
ControllerLinkBuilder
使用了这个.也复制 &粘贴.
And this one get's used by ControllerLinkBuilder
. Also copy & paste.
我的 Converter
只做 myDomainObject.getId().toString()
:
public class MyNewConverter implements Converter<Company, String> {
@Override
public String convert(Company source) {
return source.getId().toString();
}
}
现在您可以在控制器中使用复制和粘贴的 ControllerLinkBuilder
并且它按预期工作!
Now you can use the copy&pasted ControllerLinkBuilder
inside the controller and it works as expected!
这篇关于从@PathVariable DomainObject 到字符串的转换器?(使用 ControllerLinkBuilder.methodOn)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!