슈퍼 간단한의 SpringBoot 통합 Swagger2

API는 여러 손으로 쓴 문서 통증 포인트 :

  1. 문서 요구, 문서 업데이트를 적시에 교환이 아닌 것을, 다시 전면에 사본을 보낼 필요를 업데이트 할 수 있습니다.
  2. 반환 인터페이스 결과는 명확하지 않다
  3. 온라인 테스트 인터페이스를 직접 할 수 없습니다, 일반적으로 우편 배달부와 같은 도구의 사용을 필요로
  4. 너무 많은 인터페이스 문서, 가난한 관리

자신감은 자신감이 확실히 완벽 할 것이라고 말할 수 없다, 물론,이 문제를 해결하는 것입니다, 물론, 가장 명백한은 상대적으로 강한로 코드, 단점이 있습니다.

그 외에는, 당신은 자신감과 자신감의 공식 웹 사이트로 이동 할 수 있습니다 알고 싶어요, 당신은 물론, 우리가 SpringBoot 통합 Swagger2, 인터페이스 문서를 생성하는 직접적인 방법을 설명하기 위해 여기에 있습니다, 바로 자신감 편집기 인터페이스 문서를 작성할 수 있습니다.

의존의 도입

<!-- swagger2 接口文档-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>

 

자신감 구성 클래스

사실, 모든 후, 특정 충분히 구성 할 수있는 무언가로 한이 구성 클래스는,이 점은 이동 후 한 번 구성 할 필요가 없습니다. 특히 노트의 구성 파일이 API를 컨트롤러 패키지 또는 세대 문서 스캔 인터페이스보다 작은 경로입니다.

package com.gpdi.lxq.eladmin.config;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @description: 接口文档
 * @author: Lxq
 * @date: 2020/1/19 9:25
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gpdi.lxq.eladmin.rest"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxxxxxxxxxxxxx平台 APIs")
                .description("xxxxxxxxxxxxxx APIs")
                .termsOfServiceUrl("http://www.baidu.com")
                .contact(new Contact("developer", "#", ""))
                .version("1.0")
                .build();
    }
}

 

Swagger2 문서

시작 SpringBoot 프로젝트, 방문  에 http : // localhost를 : 19099 / 엘 -admin / 자신감-ui.htm

 

자신감 코멘트

주석을 통해 자신감이 인터페이스 등등 인터페이스 이름, 요청 방법, 매개 변수, 반환 정보 등을 포함, 문서를 생성을 나타냅니다.

  • @Api : 전체 클래스의 변경은, 컨트롤러의 역할을 설명
  • @ApiOperation : 한 클래스 또는 인터페이스 방법을 기술
  • @ApiParam : 단일 매개 변수 설명
  • @ApiModel는 : 오브젝트 파라미터를 수신하는
  • @ApiProperty : 오브젝트 파라미터는 오브젝트 필드에 대한 설명을 수신하면
  • @ApiResponse : HTTP 응답을 설명하는 것을 특징으로
  • @ApiResponses : HTTP 응답 전반적인 설명
  • @ApiIgnore :이 코멘트를 무시하도록 API를 사용하여
  • @ApiError : 오류가 발생할 반환
  • @ApiImplicitParam : 요청 매개 변수
  • @ApiImplicitParams : 요청 파라미터 복수

 

发布了40 篇原创文章 · 获赞 24 · 访问量 2574

추천

출처blog.csdn.net/weixin_38982591/article/details/104051625