【问题标题】:Request Header Setup in Swagger Integration With Spring Data RestSwagger 与 Spring Data Rest 集成中的请求标头设置
【发布时间】:2017-08-20 16:12:15
【问题描述】:

我在 Spring Boot 项目中为 Spring Data Rest 生成的端点设置了 Swagger 集成设置。以下是我在项目中的相关依赖:

compile "io.springfox:springfox-swagger2:2.6.1"
compile "io.springfox:springfox-swagger-ui:2.6.1"
compile "io.springfox:springfox-data-rest:2.6.1"
compile "io.springfox:springfox-bean-validators:2.6.1"

Swagger 中为这些端点设置的标题是多种多样的。例如,我有

curl -X GET --header 'Accept: application/x-spring-data-compact+json' 'http://localhost:8080/accounts'

标头中的接受或内容类型应为 application/json。如何设置正确的标题?

【问题讨论】:

  • 没有下拉框可以选择application/json或application/x-spring-data-compact+json吗?
  • 是的,有一个下拉列表。但是 application/json 或 application/+json 不存在。

标签: swagger spring-data-rest springfox


【解决方案1】:

您可以使用@ApiOperation Swagger 注解告诉 Swagger 端点可以返回的内容类型。例如;

@ApiOperation(produces = "application/json,application/x-spring-data-compact+json")

完整示例:

@RestController
class ExampleController
{
    @ApiOperation(produces = "application/json,application/x-spring-data-compact+json")
    @RequestMapping(value = "/byId", method = RequestMethod.GET)
    public ExampleResponse getById(@RequestParam String id)
    {
         ...
    }
}

通过这样做,它们将出现在下拉列表中。

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 2015-03-07
    • 1970-01-01
    • 2020-03-04
    • 2013-12-07
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多