注意:使用Spring Boot 1.4.2 + SpringFox 2.6.0
嗨,我在@RepositoryRestResource的API文档中遇到了Swagger 2表单的问题。下面的代码工作正常(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用浏览器执行时返回数据
但是在Swagger中,GET参数类型被解释为“正文”,而不是“查询”,并且表单提交(curl ... -d'foobar'...)在404中失败,尝试提交“名称”作为请求正文。所以我尝试显式设置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”已很好地保留为参数名称:-(
有谁知道如何使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);
}
我开心!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句