一、引入依赖
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
二、编写配置类
@Configuration
@EnableSwagger2 //开启Swagger2
@Profile({"dev"})//配置swagger使用环境
public class SwaggerConfig implements WebMvcConfigurer {
/**
* 构建 事项
*/
@Bean
public Docket buildDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("main")//分组
.apiInfo(buildApiInf())//api信息
.select()
.apis(RequestHandlerSelectors.basePackage("top.jiug.demo.controller"))//扫描包
.paths(PathSelectors.any())
.build();
}
/**
* 构建 api信息
*/
private ApiInfo buildApiInf(){
return new ApiInfoBuilder()
.title("jiug API")
.description("springboot demo swagger2")
.termsOfServiceUrl("http://jiug.top:8090")
.contact(new Contact("keffett", "https://blog.csdn.net/weixin_42109200", "wakinghhh@163.com"))
.build();
}
三、接口处加swagger注解
@Controller
@ResponseBody
@Api(value="用户 接口",description="用户操作",tags = "UserController")
public class UserController {
@Resource
private UserService userService;
@GetMapping(value = "/user")
@ApiOperation(value="获取所有用户信息", notes="无需入参")
public JSONObject queryList() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("status", 200);
jsonObject.put("list", userService.queryUserList());
return jsonObject;
}
四、访问
http://localhost:8080/swagger-ui.html
如图: