相关疑难解决方法(0)

从@PathVariable DomainObject转换为String?(使用ControllerLinkBuilder.methodOn)

我试图ControllerLinkBuilder.methodOn()用非String类型调用Spring ,它总是失败.我不知道使用哪种Converter以及在何处注册.

这是我的控制器:

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

}
Run Code Online (Sandbox Code Playgroud)

System.out.println(c)效果很好.我的Company域对象从数据库获取.(我正在使用DomainClassConverter)

但另一种方式不起作用: ConverterNotFoundException: No converter found capable of converting from type @PathVariable Company to type String

我只需要一个Converter<Company, String>吗?我应该在哪里注册?我在addFormatters(FormatterRegistry registry)方法中尝试了一些东西WebMvcConfigurationSupport,但它确实显示了同样的错误.但毕竟我不确定我到底尝试了什么......

spring controller converter spring-mvc spring-hateoas

6
推荐指数
2
解决办法
2572
查看次数