swagger

依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

 项目结构

 SwaggerConfig配置

@Configuration
@EnableSwagger2  //开启Swagger2
public class SwaggerConfig {

    //配置Swagger的实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(appinfo())
                .groupName("wenghuangge")//分组
                .enable(true)//是否启动swagger
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wenghaungge.controller"))//扫描指定包
                .build()
                ;
    }

    //配置Swagger信息 appInfo
    private ApiInfo appinfo(){
        Contact contact = new Contact("wenghuangge", "http://", "469701917@qq.com");
        return new ApiInfo("Api Documentation",//title信息
                "Api Documentation", //描述
                "1.0", "urn:tos",
                contact,//作者信息
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",new ArrayList<>());
    }
}

User实体类

@ApiModel("用户实体表")
public class User{
    @ApiModelProperty("id值")
    private Integer id;
    @ApiModelProperty("用户名")
    private String name;
    @ApiModelProperty("密码")
    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

访问 http://localhost:8080/swagger-ui.html

 

常用注解:

@Api():用在请求的类上,表示对类的说明
@ApiOperation():用于方法,表示一个http请求访问该方法的操作
@ApiModel():用于响应实体类上,用于说明实体作用
@ApiModelProperty:用在属性上,描述实体类的属性
@ApiImplicitParams:用在请求的方法上
@ApiImplicitParam:用于方法,表示单独的请求参数
@ApiParam():用于方法,参数,字段说明 表示对参数的要求和说明
@ApiResponses:用于请求的方法上,根据响应码表示不同响应
@ApiResponse:用在请求的方法上,表示不同的响应
@ApiIgnore():用于类或者方法上,不被显示在页面上

 

posted @ 2020-06-28 09:53  Dorom  阅读(108)  评论(0)    收藏  举报