swagger

swagger相关jar引用

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

swagger注解的使用

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.stereotype.Component;
    @Component
    @ApiModel(value = "用户类") //swagger注解
    public class User {

        @ApiModelProperty("uid")
        private Integer uid;

        @ApiModelProperty("userName")
        private String userName;

        @ApiModelProperty("userCode")
        private String userCode;
    }
@RestController
@RequestMapping("/user")
@Api("user用户相关API")
public class Controller {
    @Autowired
    private User user;

    @ApiOperation("获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用户的姓名",defaultValue="thuempire"),
            @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用户的密码",defaultValue="123456")
    })
    @ApiResponses({
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @GetMapping(value="/getUser")
    public String Login(@RequestParam("userName") String userName,@RequestParam("password") String password){
        user.setUserCode(password);
        user.setUserName(userName);
        return user.toString();
    }

}
@SpringBootApplication
@EnableSwagger2
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
五、其他swagger注解
最常用的5个注解

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段


其它若干


@ApiResponseHTTP响应其中1个描述
@ApiResponsesHTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API 


@ApiClass
@ApiError
@ApiErrors


@ApiParamImplicit
@ApiParamsImplicit

Yml配置

# 配置swagger
swagger:
    enabled: true
    base-package: cn.turing.ml.rice.alarm
    title: turing-alarm
    version: 1.0.0.SNAPSHOT
    description: 报警绑定用户
    contact:
        name: admin
    global-operation-parameters[0]:
        name: Authorization
        description: 12
        modelRef: string
        parameterType: header
        required: false
    global-operation-parameters[1]:
        name: center-auth
        description: 12
        modelRef: string
        parameterType: header
        required: false
    global-operation-parameters[2]:
        name: site-auth
        description: 12
        modelRef: string
        parameterType: header
        required: false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值