【问题标题】:Why does Spring Boot not read the PathVariable that is defined on my interface resource?为什么 Spring Boot 不读取在我的接口资源上定义的 PathVariable?
【发布时间】:2018-01-31 06:58:11
【问题描述】:

我正在使用自定义代码生成器将 REST 合同转换为 Java 接口。下面是一个生成的资源 Java 接口的示例。

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

另外,生成器创建了一个 @RestController 实现,该接口供 Spring 创建控制器 bean。

@RestController
public class CountriesResourceImpl implements CountriesResource {

    @Override
    public CountryGetResourceModel getEntity(String id) {
        return new CountryGetResourceModel();
    }
}

到目前为止一切正常。 Spring 创建 RestController bean,并将 HTTP 调用正确定向到相应的 Handler 方法(例如getEntity)。

问题是,由于某种原因,当在接口上定义路径变量时,Spring 无法解析路径变量。所有包含路径变量的调用都会被处理,但任何路径变量的方法参数都是null。如果我再给实现类加上PathVariable注解Spring就可以解析对应的值了。

有没有办法告诉 Spring 从接口方法声明中读取 PathVariable,就像读取 RequestMapping 注释一样?

【问题讨论】:

标签: java spring spring-mvc spring-boot


【解决方案1】:

不幸的是,这似乎是一个已知问题。我在这个问题上找到了the following opened Jira。可以从this answer 找到一些解释。另外,在the same question 中,我接受了我看到this solution 的上一个答案。不过没试过,好像很重。

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2020-02-15
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多