spring-boot引入swagger2之操作步骤

  1. 引入依赖

    <!-- swagger 用于生成接口API -->
    <dependency>
    	<groupId>io.springfox</groupId>
    	<artifactId>springfox-swagger2</artifactId>
    	<version>2.8.0</version>
    </dependency>
    
    <dependency>
    	<groupId>io.springfox</groupId>
    	<artifactId>springfox-swagger-ui</artifactId>
    	<version>2.8.0</version>
    </dependency>
    
  2. 配置Swagger2配置类,样例如下

    @Configuration
    @EnableSwagger2
    public class SwaggerApi {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.test"))
                    .paths(PathSelectors.any())
                    .build()
                    .globalOperationParameters(setHeaderToken());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("API")
                    .version("1.0")
                    .build();
        }
    }
    
  3. 编写代码,样例如下

    @ApiOperation(value="测试数据")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "profiles", value = "所属系统,默认为common,public,多个使用逗号分隔,如:common,public", defaultValue = "common,public", required = false, dataType = "String", paramType="query"),
        @ApiImplicitParam(name = "keys", value = "配置项key,多个使用逗号分隔,如:key1,key2", required = true, dataType = "String", paramType="query"),
        @ApiImplicitParam(name = "defaultValues", value = "默认值,与keys一一对应,如value1,value2", dataType = "String", paramType="query")
    })
    @ApiResponses({ @ApiResponse(
    		code = 200, 
    		message = "{\"{key1}\":{value1},\"{key2}\":{value2},...}") })
    @RequestMapping(value = "/getProperty", method={RequestMethod.GET, RequestMethod.POST})
    public Map getProperty(HttpServletRequest request) throws IOException {
        return null;
    }
    
发布了43 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/u013084266/article/details/100105751