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

3

NB: 使用Spring Boot 1.4.2 + SpringFox 2.6.0

你好,我在@RepositoryRestResource上的API文档中使用Swagger 2表单出现了问题。以下代码可以正常工作(REST访问OK):

@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 可以返回数据。

但是在 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”作为参数名称在表单中很好地保留,但仍然没有成功 :-(

GET查询的正文参数类型

有人知道如何使Swagger表单工作吗?谢谢你的帮助。

1个回答

2

这就是它:@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);

}

我很高兴!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接