swagger2 @ApiImplicitParams

关于Token问题
考虑到安全的问题,每次请求API需要对用户进行验证与授权。目前主流的验证方式采用请求头部(request header)传递token,即用户登录之后获取一个token,然后每次都使用这个token去请求API。如果想利用swagger-UI进行API测试,必须显式为每个需要验证的API指定token参数。这时可以为每个操作添加一个注解@ApiImplicitParams,具体代码如下:

@ApiImplicitParams({@ApiImplicitParam(name = "TOKEN", value = "Authorization token", required = true, dataType = "string", paramType = "header")})

作者:杨梅泡酒
链接:https://www.jianshu.com/p/b0b19368e4a8
来源:
著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", paramType = "header")
    })

swagger2 注解说明

3.1、@ApiImplicitParams、@ApiImplicitParam:方法参数的说明

@ApiImplicitParams:用在请求的方法上,包含一组参数说明
@ApiImplicitParam:对单个参数的说明
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(请求体)--> @RequestBody User user
· form(普通表单提交)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值

@ApiImplictParams和@ApiImplictParam可以替换为@ApiParam

@ApiOperation(value = "试卷下拉列表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, response = CommonSelectResponse.class, message = "试卷下拉列表"),
            @ApiResponse(code = 400, response = ApiErrors.class, message = "客户端错误")
    })
    @GetMapping(value = {
            "/xueqing/common/testPaper"
    })
    @CrossOrigin
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "subjectId", value = "学科ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "gradeId", value = "学期ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "classId", value = "班级ID, 例如 1. 单个class: _24469_ 2.多个class _2030_,_8189_,_8190_ 中间用,号分割", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "格式: yyyy-MM-dd HH:mm:ss", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "yyyy-MM-dd HH:mm:ss", paramType = "query"),
    })
    public ResponseEntity getKnowledgeChart(@RequestParam(required = false) String schoolId,
                                               @RequestParam(required = false) String teacherId,
                                               @RequestParam String subjectId,
                                               @RequestParam String gradeId,
                                               @RequestParam String classId,
                                               @RequestParam(required = false) String studentId,
                                               @RequestParam(required = false) String startDate,
                                               @RequestParam(required = false) String endDate) {
//        List dto = new ArrayList<>();
//        CommonSelectDTO CommonSelectDTO = new CommonSelectDTO("a0001", "七年级701班第1次数学考试");
//        CommonSelectDTO CommonSelectDTO1 = new CommonSelectDTO("a0002", "七年级701班第2次数学考试");
//        dto.add(CommonSelectDTO);
//        dto.add(CommonSelectDTO1);
//        CommonSelectResponse result = new CommonSelectResponse(dto, null);
        CommonSelectResp examPaperList = dropDownListService.getExamPaperList(subjectId, gradeId, classId, startDate, endDate);
        return ResponseEntity.ok(examPaperList);
    }

    @ApiOperation(value = "试卷下拉列表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, response = CommonSelectResponse.class, message = "试卷下拉列表"),
            @ApiResponse(code = 400, response = ApiErrors.class, message = "客户端错误")
    })
    @GetMapping(value = {
            "/xueqing/common/testPaper"
    })
    @CrossOrigin
    public ResponseEntity getKnowledgeChart(
            @RequestParam @ApiParam(required = true, name = "subjectId", value = "学科ID 例如: 2") String subjectId,
            @RequestParam @ApiParam(required = true, name = "gradeId", value = "学期ID 例如: 9") String gradeId,
            @RequestParam @ApiParam(required = true, name = "classId", value = "班级ID 例如:  \\_8189\\_ ") String classId,
            @RequestParam(required = false) @ApiParam(required = false, name = "startDate", value = "格式: yyyy-MM-dd HH:mm:ss") String startDate,
            @RequestParam(required = false) @ApiParam(required = false, name = "endDate", value = "yyyy-MM-dd HH:mm:ss") String endDate) {
//        List dto = new ArrayList<>();
//        CommonSelectDTO CommonSelectDTO = new CommonSelectDTO("a0001", "七年级701班第1次数学考试");
//        CommonSelectDTO CommonSelectDTO1 = new CommonSelectDTO("a0002", "七年级701班第2次数学考试");
//        dto.add(CommonSelectDTO);
//        dto.add(CommonSelectDTO1);
//        CommonSelectResponse result = new CommonSelectResponse(dto, null);
        CommonSelectResp examPaperList = dropDownListService.getExamPaperList(subjectId, gradeId, classId, startDate, endDate);
        return ResponseEntity.ok(examPaperList);
    }

你可能感兴趣的:(swagger2 @ApiImplicitParams)