一、在pom.xml加入Swagger jar包引入

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

  二、配置Swagger 配置类

package bp.config;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.containsPattern; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.google.common.base.Predicate; 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.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("bp.controller"))
.paths(paths())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("城市营商SAAS系统接口API")
.description("API接口规则:必须要以/api开头")
.version("1.0")
.build();
} @SuppressWarnings("unchecked")
private Predicate<String> paths(){
return and(containsPattern(".*api.*"));
} }

  三、controller类

@RestController
@RequestMapping(value="/api",method={RequestMethod.GET})
@Api("行政事项页面接口")
public class AdministrativeMattersController extends BaseController { @ApiOperation(value = "描述", httpMethod = "GET", notes = "描述信息")
@RequestMapping(value = "接口访问路径", method = RequestMethod.GET)
public BaseModel getAdministrativeByDepartment(@ApiParam(required=true, name="city", value="参数说明") @RequestParam(name = "city", required=true) String city){
Map<String,Object> result = new HashMap<String,Object>();
retrun result;
} }

四、启动项目 访问 http://ip:端口/swagger-ui.html 即可成功进行swagger接口测试。

05-11 22:27