Swagerプロジェクトの適用

14019 ワード

Swaggerの説明


使用状況

  • は、他の開発チームと連携する場合、APIドキュメント
  • として使用することができる.
  • 導入済みプロジェクトの保守
  • バックエンドのAPIを呼び出すためにフロントエンド・プログラムを作成している場合は、次の手順に従います.
    - Web Front, Android, IOS ... などすべて
  • 長所

  • API情報実行可能
  • APIにより、パラメータ、応答情報、例などのSpec情報を容易に伝達することができる.
  • は、実際に使用するパラメータテスト
  • に合格することができる.
  • に別途ログインする必要はありません.
    -実際の運用では、セキュリティを高めるためにjwtなどのトークンを使用してログインして使用します.
  • ステージ

  • バージョン管理ツールにSwagerを追加する
  • Swaggerプロファイルクラス
  • を追加
  • コントローラおよびDTOへの説明
  • の追加

    結果




    1. Maven - Pom.xmlにSwaggerを追加

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${springfox-swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${springfox-swagger.version}</version>
            </dependency>

    2.Swagger構成クラスの追加

    @EnableSwagger2
    @Configuration
    public class SwaggerConfig {
    
    	@Bean
    	public Docket api() {
    		return new Docket(DocumentationType.SWAGGER_2)
    						.select()
    						.apis(RequestHandlerSelectors.any())
    						.build()
    						.pathMapping("/")
    						.apiInfo(metaData());
    	}
    
    	private ApiInfo metaData() {
    		Contact contact = new Contact("성원준", "https://velog.io/@donsco", "[email protected]");
    
    		return new ApiInfo("",
    						"Spring Framework CAD",
    						"1.0",
    						"Terms of Service: ...",
    						contact,
    						"Apache License Version 2.0",
    						"https://www.apache.org/licenses/LICENSE-2.0",
    						new ArrayList<>());
    	}
    }
  • Swagerバージョンは3つあり、2つのバージョンを使用する場合は@EnableSwager 2を追加します.
  • Swaggerドキュメントの上部に追加情報(メタデータ)を挿入すると、ApiInfoメソッドを作成してDocketに貼り付けることができます.
  • 3.コントローラとDTOに説明を追加


    Controller

    @Api(description = "고객 컨트롤러")
    @RestController
    @RequestMapping(CustomerController.BASE_URL)
    public class CustomerController {
    
        public static final String BASE_URL = "/api/v1/customers";
    
        private final CustomerService customerService;
    
        public CustomerController(CustomerService customerService) {
            this.customerService = customerService;
        }
    
        @ApiOperation(value = "간략 설명", notes = "상세 설명")
        @GetMapping
        @ResponseStatus(HttpStatus.OK)
        public CustomerListDTO getListOfCustomers(){
            return new CustomerListDTO(customerService.getAllCustomers());
        }
    
    ...

  • コントローラ上部に@Apiプロンプトと説明を追加し、説明を追加します.

  • メソッドでは@ApiOperation宣言を使用して詳細に説明できます.

  • descriptionは現在Depecifiedになっているのでtags方式をお勧めします.
    Swagger @Api Description is Deprecated
    Api annotation's description is deprecated
  • DTO

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class CustomerDTO {
    
        @ApiModelProperty(value = "first name은 ~~입니다.", required = true)
        private String firstname;
    
        @ApiModelProperty(required = true )
        private String lastname;
    
        @JsonProperty("customer_url")
        private String customerUrl;
    }
  • @ApiModelPropertyを使用して、値に必要な説明値を入力します.
  • requiredをtrueに設定すると、カラムは必須フィールドとして指定されます.