【问题标题】:@ApiParam is not working with springfox swagger 2@ApiParam 不适用于 springfox swagger 2
【发布时间】:2017-09-21 15:03:37
【问题描述】:

我在我的 api 文档中使用 Springfox swagger 2,并且我有以下控制器:

HelloController.java

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wordnik.swagger.core.Api;
import com.wordnik.swagger.core.ApiParam;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;


@RestController
@RequestMapping(value="/hello",method = RequestMethod.GET)
@Api(value="Hello wwwww", description="this controller will say hello")


public class HelloController {
    //@ApiIgnore
    @ApiOperation("Get all products of our database")
    @RequestMapping(value = "/id", method = RequestMethod.GET)

    private String hello(@ApiParam(name = "studentId", 
              value = "The Id of the Student to be viewed", 
              required = true) 
    @PathVariable ("student") Integer studentId) {
        String name = "hello world";
      return name;
    }

}

所以问题是我的 swagger ui 中没有正确显示参数描述,知道为什么吗?

提前谢谢你。

【问题讨论】:

    标签: java spring swagger swagger-ui swagger-2.0


    【解决方案1】:

    这与 @Api 注释的 description() 方法已弃用并记录为:“未在 1.5.X 中使用,保留用于遗留支持的事实有关。” .

    这已经在 github 上讨论过 swagger :swagger 2.0 why api description has been deprecated ?

    替代方法是使用@SwaggerDefinition

    【讨论】:

    • 感谢您的回复,我使用的是 2.7.0 版本,并且 (@ApiOperation) 工作正常,注释未显示为已弃用,它们正常显示,只有 (@apiparam) 没有'没有给出正确的参数描述,有没有其他注释可以改变参数描述?
    【解决方案2】:

    如果您想在 Swagger 的 @Api 中进行描述,您应该使用 tags 例如:

    @Api(tags = {"external_info","user_info"})

    grouping with tags:

    您可以使用根级别的全局标签部分为每个标签指定描述和外部文档。此处的标签名称应与操作中使用的名称匹配。

    在你的情况下

    @Api(tags = {"hello_info"})
    

    并指定标签:

    tags:
      - name: hello_info
        description: this controller will say hello
    

    或在@SwaggerDefinition(

       tags = {
                @Tag(name = "hello_info", description = "this controller will say hello")
        }, 
    

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2022-01-07
      • 2018-04-29
      • 2020-04-05
      相关资源
      最近更新 更多