我正在使用自定义代码生成器将 REST 合约转换为 Java 接口(interface)。下面是生成的资源 Java 接口(interface)的示例。
@Generated(
date = "2018-01-30T11:56:25.156Z",
comments = "Specification filename: country.v1.json",
value = "GENERATOR"
)
@RequestMapping("/v1/countries")
public interface CountriesResource {
@RequestMapping(
method = RequestMethod.GET,
path = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseBody
LandGetResourceModel getEntity(@PathVariable("id") String id);
}
此外,生成器还为 Spring 创建一个该接口(interface)的 @RestController
实现来创建 Controller bean。
@RestController
public class CountriesResourceImpl implements CountriesResource {
@Override
public CountryGetResourceModel getEntity(String id) {
return new CountryGetResourceModel();
}
}
到目前为止,一切正常。 Spring 创建 RestController bean,并且 HTTP 调用被正确定向到相应的 Handler 方法(例如 getEntity
)。
问题在于,由于某种原因,当在接口(interface)上定义路径变量时,Spring 无法解析它们。所有包含路径变量的调用都会被处理,但任何路径变量的方法参数都是null
。如果我随后将 PathVariable
注解添加到实现类,Spring 就能够解析相应的值。
有没有办法告诉Spring从接口(interface)方法声明中读取PathVariable
,就像读取RequestMapping
注释一样?
最佳答案
不幸的是,这似乎是一个已知问题。我找到了the following opened Jira关于这个问题。一些解释可以从 this answer 找到。另外,在 the same question我采取了我之前看到的答案this solution 。不过我没试过,感觉好像很重。
关于java - 为什么 Spring Boot 不读取我的接口(interface)资源上定义的 PathVariable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48536260/