swagger(絲襪哥) - 讓你更好的書寫API文檔的框架

  1. 首先須要引入幾個maven依賴,pom.xml以下
    <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.0-jre</version>
        </dependency>
  2. 配置config
    @EnableSwagger2
    @Configuration
    public class SwaggerConfig {
    
    	@Bean
    	public Docket swaggerSpringMvcPlugin() {
    		return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    	}
    
    }

     

  3. 配置controller
    public class InterfaceDocumentation {
        /**
         * 建立我的帳號,返回的accountId會在建立企業帳號時須要
         *
         * @param name   姓名
         * @param idNo   證件號
         * @param idType 我的帳號證件類型
         * @param mobile 手機號
         * @param email  郵箱
         * @return
         */
        @ApiOperation(value = "建立我的帳號", notes = "建立企業帳號以前必需要先調用此接口建立我的帳號")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "name", value = "用戶姓名", required = true, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "idNo", value = "證件號", required = true, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "idType", value = "我的帳號證件類型", required = true, dataType = "int", paramType = "query"),
                @ApiImplicitParam(name = "mobile", value = "手機號", required = true, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "email", value = "郵箱", required = false, dataType = "String", paramType = "query"),
        })
        @RequestMapping(value = "/account/create/person", method = RequestMethod.POST)
        public String createPersonAccount(@RequestParam("name") String name, @RequestParam("idNo") String idNo,
                                                         @RequestParam("idType") Integer idType, @RequestParam("mobile") String mobile,
                                                         @RequestParam(value = "email", required = false) String email) {
          return "";
        }
    }
  4. 效果圖展現

注意事項:當springboot中使用了thymeleaf模板時訪問swagger-ui.html出現404或重定向問題,加上如下代碼便可html

相關文章
相關標籤/搜索