Sprint Boot之配置Swagger

一、Swagger资源下载

①在GitHub上下载SwaggerUI项目,将dist下所有内容拷贝到本地项目/webapp下面。
Sprint Boot之配置Swagger_第1张图片
②修改其中的index.html文件将 url = “http://petstore.swagger.io/v2/swagger.json“; 改为url=”/v2/api-docs”;

二、添加依赖

compile('io.springfox:springfox-swagger2:2.8.0')
compile('io.springfox:springfox-swagger-ui:2.8.0')
compile 'io.swagger:swagger-jersey2-jaxrs:1.5.8'
compile('com.mangofactory:swagger-springmvc:1.0.2')
compile('com.mangofactory:swagger-models:1.0.2')
compile('com.wordnik:swagger-annotations:1.3.11')*

三、添加配置文件

package cn.cityworks.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.HashSet;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.cityworks"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {//访问Swagger的地址
        return new ApiInfoBuilder()
                .title("APi接口")
                .description("增删改查服务")
                .termsOfServiceUrl("http://192.168.2.42:9055/swagger-ui.html")
                .version("1.0").build();
    }

    @Bean
    public Docket MapperApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("MapperApi接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.cityworks.controller"))
                .paths(PathSelectors.any()).build();
    }
}

四、Controll层添加注解

①Controll类顶部添加注解

@Api(tags="账户")//Swagger注解
@RestController
public class AccountController {

}

②方法添加注解和字段描述(字段描述依据个人字段添加,令牌access_token是本人项目所需)

    @ApiOperation(value="ID查询账户")

    @ApiImplicitParams({    //字段描述
    		@ApiImplicitParam(name = "id",value = "ID",required = true,dataType = "String",paramType="query"),//字段
    		//添加头部access_token令牌
            @ApiImplicitParam(paramType = "header", name = "access_token", value = "访问令牌", required = true, dataType = "String")
            })
//Swagger↑

//底下正常写法
    @RequestMapping(value = "/account/findAccountByID", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String findAccountByID(@RequestParam("id") String id) {
        Account account = accountService.findAccountByID(id);
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(account);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{\"msg\":\"error\"}";
    }
    

示例:Controller类大致如下
Sprint Boot之配置Swagger_第2张图片

你可能感兴趣的:(Swagger)