转换器从@PathVariable DomainObject到String? (使用ControllerLinkBuilder.methodOn) [英] Converter from @PathVariable DomainObject to String? (using ControllerLinkBuilder.methodOn)

查看:382
本文介绍了转换器从@PathVariable DomainObject到String? (使用ControllerLinkBuilder.methodOn)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用非字符串类型调用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.

这里是我的Controller:

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));
    }

}

System.out.println(c)运行良好。我的公司域对象get从数据库获取。 (我使用 DomainClassConverter

The System.out.println(c) works well. My Company Domain object get's fetched from DB. (I'm using DomainClassConverter)

但是其他方式不工作: ConverterNotFoundException:没有转换器能够从类型@PathVariable转换公司到类型字符串

But the other way doesn't work: ConverterNotFoundException: No converter found capable of converting from type @PathVariable Company to type String

我只需要一个 ; Company,String> ?我应该在哪里注册?我尝试了 addFormatters(FormatterRegistry注册表)方法 WebMvcConfigurationSupport ,但它只是显示相同的错误。但毕竟我不知道我究竟是试图...

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...

推荐答案

找到一个解决方案。它需要很多复制&

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());
    }

    ...
}

此类get由 ControllerLinkBuilderFactory 使用。所以我不得不复制&

This class get's used by ControllerLinkBuilderFactory. So I had to copy & paste that, too.

这个get被 ControllerLinkBuilder 使用。还复制& 只需 myDomainObject.getId()。toString()

And this one get's used by ControllerLinkBuilder. Also copy & paste.

/ code>:

public class MyNewConverter implements Converter<Company, String> {
    @Override
    public String convert(Company source) {
        return source.getId().toString();
    }   
}

现在您可以使用复制& pasted ControllerLinkBuilder 在控制器里面,它工作正常!

Now you can use the copy&pasted ControllerLinkBuilder inside the controller and it works as expected!

这篇关于转换器从@PathVariable DomainObject到String? (使用ControllerLinkBuilder.methodOn)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆