SpringBoot Swager設定


スワーガーとは?


Swagerは、開発されたREST APIを容易に記録し、サードパーティのユーザを管理およびテストすることによって、APIを容易に呼び出しおよびテストすることができるAPIである.
SpringBootは簡単にspringfox-boot-starterをmaven、grade依存項目に追加できます.
ただし、注意すべき点は、運用環境などの外部環境では使用できないことです.

実施方法


springboot version

id 'org.springframework.boot' version '2.4.4'

maven repository



dependacy


springfoot 2ベースspringfox-swagger2springfox-swagger-ui2のパンダ市が必要です.
  • maven
  • <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>
    </dependency>
  • gradle
  • // https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
    

    ちょうせい

    @Api:クラスを自慢のリソースとしてマーク@ApiOperation:特定経路の動作Http方法を記述する@ApiResponse:指定された製品の応答
    @ApiOperation(value="AI 전기방식 동작여부 확인", notes="시스템에 등록된 현재 AI 전기방식 상태값 리턴(state:'Y' or 'N'). '../set-state'가 일정 시간 호출되지 않을 경우 N을 리턴.")
    @ApiResponses({
            @ApiResponse(code = 200, message = "API 정상 작동"),
            @ApiResponse(code = 500, message = "서버 에러")
    })
    @RequestMapping(value="/is-running", method={RequestMethod.GET})
    public ResponseEntity<AIRunningVo> isRunning() throws Exception {
        AIRunningVo res = new AIRunningVo();
        res.setState(AiStateManager.getInstance().getState());
    
        return new ResponseEntity<AIRunningVo>(res, HttpStatus.OK);
    }
    @ApiParam:メタデータを製品パラメータに記述する@ApiModelProperty:モデルの属性データを記述する
    @Data
    public class AmiDeviceAddVo {
        @ApiModelProperty(value = "계량기번호", dataType = "string", required = true)
        String amiId;
        @ApiModelProperty(value = "검인일자")
        String bglnum;
        @ApiModelProperty(value = "고객센터")
        String ccenter;
    	...
    }

    インプリメンテーションソース


    SwaggerConfig
    :@EnableSwagger2使用
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("springBoot-swagger-example ")
                    .description("description")
                    .version("1.0")
                    .build();
        }
    }

    Webブラウザの確認


    Webブラウザでhttp://localhost:8080/swagger-ui.htmlを検索すればいいです.

    テスト


    valueを入力してTry it outをクリックします

    [Execute]をクリックします.

    応答本体、応答コードなどの結果を表示できます.

    リファレンス


  • ソース:https://github.com/mooh2jj/springboot-swagger-example.git

  • https://nahwasa.com/entry/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-Swagger-UI-300-%EC%A0%81%EC%9A%A9-%EB%B0%A9%EB%B2%95-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-22-%EC%9D%B4%EC%83%81-Spring-Boot-Swagger-UI