Swagger2 소개

One, Swagger2 소개

프런트 엔드 및 백 엔드 개발 모델에서 api 문서는 의사 소통을위한 가장 좋은 방법입니다.

Swagger는 RESTful 웹 서비스를 생성, 설명, 호출 및 시각화하기위한 표준화되고 완전한 프레임 워크입니다.

及时性 (接口变更后,能够及时准确地前后通知相关端开发人员)
规范性 (并且保证接口的规范性,如接口的,请求地址方式,参数及响应格式和错误信息)
一致不会出性 (接口信息一致,现因开发人员拿到的文档版本不一致,而出现分歧)
可测性 (直接在接口文档上进行测试,以方便理解业务)

둘째, Swagger2 구성

1. 공통 모듈 만들기

guli-parent 아래에 공통 모듈 만들기

구성 :

groupId : com.atguigu

artifactId : 공통

2. 공통적으로 관련된 의존성 도입

org.springframework.boot spring-boot-starter-web 제공
    <!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--lombok用来简化实体类:需要安装lombok插件-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <scope>provided </scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <scope>provided </scope>
    </dependency>

    <!-- redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- spring2.X集成redis所需common-pool2
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.0</version>
    </dependency>-->
</dependencies>

3. 공통 아래에 서브 모듈 서비스 기반 생성

3. 모듈 서비스 기반에서 swagger에 대한 구성 클래스를 만듭니다.

com.atguigu.servicebase.config 패키지 생성, SwaggerConfig 클래스 생성

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }
    
    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("Helen", "http://atguigu.com", "[email protected]"))
                .build();
    }
}

4. 모듈 서비스 모듈에 서비스 기반 도입

com.atguigu 서비스 기반 0.0.1-SNAPSHOT

5. 테스트를 위해 service-edu 시작 클래스에 주석 추가

6. API 모델

다음과 같은 일부 사용자 지정 설정을 추가 할 수 있습니다.

샘플 데이터 정의

@ApiModelProperty(value = "创建时间", example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;

@ApiModelProperty(value = "更新时间", example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;

5. 인터페이스 설명 및 매개 변수 설명 정의

클래스에 정의 : @Api는
메소드에 정의됩니다. @ApiOperation은
매개 변수에 정의됩니다. @ApiParam

@Api(description="讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherAdminController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public List<Teacher> list(){
        return teacherService.list(null);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public boolean removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        return teacherService.removeById(id);
    }
}

여기에 사진 설명 삽입
그림과 같이 인터페이스를 참조하십시오.

추천

출처blog.csdn.net/he1234555/article/details/115048320