swagger

swagger相关jar引用

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.7.0version>
        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

你可能感兴趣的:(swagger)