【swagger-ui】【swagger-bootstrap-ui】生成接口文档

依赖

<!--swagger相关依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
            <version>2.9.2</version>
        </dependency>
        <!--swagger防止报错Illegal DefaultValue for parameter type integer-->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>
        <!--增强swagger-bootstrap-ui-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.2</version>
        </dependency>

配置类

SwaggerConfig


@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig{
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                //apiInfo指定测试文档基本信息,这部分将在页面展示
                .apiInfo(apiInfo())
                .select()
                //apis() 控制哪些接口暴露给swagger,
                // RequestHandlerSelectors.any() 所有都暴露
                // RequestHandlerSelectors.basePackage("com.info.*")  指定包位置
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Swagger-UI")
                .description("API接口描述")
                //联系人实体类
                .contact(
                        new Contact("name", "url", "email")
                )
                //版本号
                .version("v1.0")
                .build();
    }
}

WebConfig

@Configuration
public class WebConfig implements WebMvcConfigurer  {

    /**
     * 不配置 /swagger-ui.html访问会404
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("doc.html");
    }


}

注解

@RestController
@RequestMapping("award")
@Api(value = "项目信息", tags = "项目信息管理")
public class ProjectController {
	@PostMapping(("/{id}"))
    @ApiOperation("根据id查看信息")
    @ApiImplicitParam(name = "id", value = "项目id", paramType = "path", dataType = "int", required = true)
    public JsonResult get(@PathVariable("id") Integer id) {
        return JsonResult.ok(userService.selectProjectById(id));
    }
    
    @PostMapping()
    @ApiOperation(value = "新增项目",notes="json格式")
    public JsonResult add(@RequestBody Project project) {
        return JsonResult.ok(userService.addProject(project));
    }
    
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象", paramType = "form", dataType = "file", required = true),
            @ApiImplicitParam(name = "project", value = "项目名称", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "isEdit", value = "是否", paramType = "form", dataType = "boolean", required = true),
    })
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam("project") String project, @RequestParam("isEdit") Boolean isEdit) throws IOException {
        ...
    }



}

针对参数为@RequestBody 时

@Data
@TableName("project")
@ApiModel("项目信息")
public class Project implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    @ApiModelProperty(value = "id", required = true)
    private Integer id;

    @TableField("name")
    @ApiModelProperty(value = "项目名称", required = true)
    private String name;

    @TableField("status")
    @ApiModelProperty(value = "审核状态", required = false)
    private String status;

    @TableField("datetime")
    @ApiModelProperty(value = "变更日期", hidden = true)
    private String datetime;


}

查看接口文档

地址:http:ip:port/doc.html

  • 可查看所有接口信息
  • 可进行接口调试

在这里插入图片描述

注解说明

详细说明:swagger2 注解说明
@Api: 用于controller类上

@Api:放在 请求的类上,与 @Controller 并列,说明类的作用
tags=“说明该类的作用”
value=“该参数没什么意义,所以不需要配置”

@ApiOperation: 用于方法上

@ApiOperation:“用在请求的方法上,说明方法的作用”
value=“说明方法的作用”
notes=“方法的备注说明”

@ApiImplicitParams、@ApiImplicitParam: 用于方法上

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

@ApiResponses、@ApiResponse: 用于方法上

@ApiResponses:方法返回对象的说明
@ApiResponse:每个参数的说明
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类

@ApiModel、@ApiModelProperty: 用在JavaBean类上

@ApiModel:用于JavaBean上面,表示对JavaBean 的功能描述
value:对类的说明、描述
@ApiModelProperty:用在JavaBean类的属性上面,说明属性的含义
value:属性的说明、描述
required:是否必须必填
hidden:是否隐藏

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值