Swagger

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

main中加入注解启动
Swagger

访问地点:

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

配置文件

package com.example.swaggerlearn.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment)
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //配置PAI分组
                .groupName("TokNan")
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggerlearn.controller"))
                .build();
    }


    Contact contact=new Contact("孙亚楠","123.56.42.123","2473183730@qq.com");

    private ApiInfo apiInfo(){
        return new ApiInfo(
                "TokNan的Swagger文档",
                "去面对,而不是逃避",
                "1.0",
                "urn:tos",
                contact ,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    }
}

实体类

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;
    @ApiModelProperty("用户编码")
    private Integer id;
}

方法

@Api(description = "Hello控制类")
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello()
    {
        return "Hello World";
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/getuser")
    public User getUser(@ApiParam("用户名") String name)
    {
        return new User("sunyanan","190708116",12);
    }
}

更换ui
bootstrap-ui 访问 http://localhost:8080/doc.html

<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>swagger-bootstrap-ui</artifactId>
   <version>1.9.1</version>
</dependency>
上一篇:Swagger-UI简单使用


下一篇:Spring boot集成Swagger