1、导入pom文件jar依赖
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、配置swagger配置文件
package com.reset.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created by chendai on 2018/3/21.
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 当前包路径
.apis(RequestHandlerSelectors.basePackage("com.reset.controller"))
.paths(PathSelectors.any()).build();
}
//构建api文档的详细信息函数
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
//页面标题
.title("springBoot测试使用Swagger2构建RESTful API")
//创建人
.contact(new Contact("chendai","http://www.baidu.com",""))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
3、配置controller层
package com.reset.controller;
import com.reset.model.RestMessgae;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
/**
* Created by chendai on 2018/3/19.
*/
@RestController
@Api("swaggerTestController相关api")
public class TestController {
/**
* Restful Get请求测试
*/
@ApiOperation(value = "根据id查询学生的信息",notes = "查询数据库中某个学生的信息")
@ApiImplicitParam(name ="id",value = "学生id",paramType = "path",required = true,dataType = "String")
@ApiImplicitParams({
@ApiImplicitParam(name = "id",value = "用户id",dataType = "String",paramType = "query",example = "1112")
})
@ApiResponses({
@ApiResponse(code=400,message = "请求参数没有填好"),
@ApiResponse(code=404,message="请求路径没有找到")
})
@GetMapping(value = "testRest/{id}")
public RestMessgae testGetResetful(@PathVariable String id){
RestMessgae restMessgae = new RestMessgae();
System.out.println(id);
return restMessgae;
}
}
5、输入入参,点击try it out 按钮验证
6、如果感觉controller层的配置不太理解,可以配合swagger页面对应参数