Spring Data Rest,SpringFox 和 JpaRepository 自定义查找器

Spring Data Rest, SpringFox and JpaRepository custom finders

注意:使用 Spring Boot 1.4.2 + SpringFox 2.6.0

您好,我的 API 文档中的 Swagger 2 表单在 @RepositoryRestResource 上遇到问题。下面的代码工作正常(REST 访问正常):

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends JpaRepository<Person, Long> {
    Person findByLastName(@Param("name") String name);
}

而且 HATEOAS 链接也是正确的:调用 URL /api/people/search 以这个结束(注意参数 "name"):

{
  "_links": {
    "findByLastName": {
      "href": "http://localhost:8080/api/people/search/findByLastName{?name}",
      "templated": true
    },
    "self": {
      "href": "http://localhost:8080/api/people/search"
    }
  }
}

REST API没问题:URL /api/people/search/findByLastName?name=foobar returns data when execution with a browser

但是在 Swagger 中,GET 参数类型 被解释为 "body" 而不是 "query" 并且表单提交(curl ... -d 'foobar'...) 在 404 中失败,尝试提交 "name" 作为请求正文。 所以我尝试显式设置 Swagger,如下所示:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends JpaRepository<Person, Long> {
    @ApiOperation("Find somebody by it's last name")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", paramType = "query")
    })
    Person findByLastName(@Param("name") @ApiParam(name = "name") String name);
}

没有任何成功,尽管 "name" 在这个例子中作为参数名称被很好地保留在表单中:-(

body parameter type on GET query

有谁知道如何使 Swagger 表单发挥作用?谢谢你的帮助

就是这样:@Param 配置 Spring Data REST,而 @RequestParam 适合 Swagger

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends JpaRepository<Person, Long> {

    // @Param Spring Data REST : Use @Param or compile with -parameters on JDK 8
    // @RequestParam Swagger : paramType=query cf. $Api*Param

    Person findByLastName(@Param("name") @RequestParam("name") String name);

}

我很开心!