Swagger

作者: 请叫我平爷 | 来源:发表于2022-02-23 14:43 被阅读0次

knife4j 版本

两种访问方式

  1. http://localhost:8001/swagger-ui.html
  2. http://localhost:8001/doc.html

新项目集成

pom.xml

    <!--springfox-swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

    <!-其他依赖-->
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>

SwaggerConfiguration:编写配置文件SwaggerConfiguration

package com.example.demo.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui RESTful APIs")
                .description("swagger-bootstrap-ui")
                .termsOfServiceUrl("http://localhost:8999/")
                .contact("developer@mail.com")
                .version("1.0")
                .build();
    }
}

ApiModelProperty 使用

package com.example.demo.bean;

import com.example.demo.annotations.AutoDocField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@Data
public class SwaggerReqBean {

    @ApiModelProperty("课程")
    private String name;

    @ApiModelProperty("课程")
    private int age;

    @ApiModelProperty("课程")
    private List<SwaggerSubBean> classList;

}

ApiOperation使用

    @ApiOperation(value = "ApiOperation请求")
    @PostMapping("/apiOperation")
    @ResponseBody
    public String apiOperation(@RequestBody SwaggerReqBean reqBean){
        return "this is post request : "+ JSON.toJSONString(reqBean);
    }

访问

这时候就可以通过http://localhost:8001/doc.html 访问到页面了

相关文章

网友评论

      本文标题:Swagger

      本文链接:https://www.haomeiwen.com/subject/vjjylrtx.html