java - 为什么 Spring Boot 不读取我的接口(interface)资源上定义的 PathVariable?

标签 java spring spring-mvc spring-boot

我正在使用自定义代码生成器将 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/

相关文章:

java - org.mule.tools.maven 中发生无法解决的错误

spring - Angular 4 http 获取多个参数到 Spring 请求映射

mysql - 使用 hibernate 注释从 jsp 中的下拉列表获取值到 spring mvc Controller

java - Spring MVC @Controller 方法签名

Spring @InitBinder for joda 时间

java - Sprite 是在背景下绘制的

尝试在服务器中运行时出现 Java.lang.illegalArgumentException

java - 运行 while 语句后结束 Java 线程

java - Spring Security 循环 bean 依赖

java - Spring Controller 将输出返回到 View 时出现错误