增加依赖:
在pom.xml文件中增加加入Swagger2的依赖:记得在maven中install一下,如果还报红,则重启idea
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.13</version>
</dependency>
创建Java类:
在包中创建一个package:config,里面创建一个类:SwaggerConfig.java,(这个包和类的位置没有要求)
package com.example.mybatisplus.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
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;
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.mybatisplus.web.controller"))
.paths(PathSelectors.any())
.build();
}
//RequestHandlerSelectors.basePackage(接口所在包路径)
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("springmvc swagger2")
.contact("moyu")
.version("1.1")
.build();
}
}
在controller类中增加注解:
@Controller
@RequestMapping("/api/admin")
@Api(value = "api接口", description="用户相关操作")
public class AdminController {
private final Logger logger = LoggerFactory.getLogger( AdminController.class );
@Autowired
private AdminService adminService;
/**
* 描述:根据Id 查询
*
*/
@ApiOperation(value="查询用户", notes="根据url的id来查询指定对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
Admin admin = adminService.getById(id);
return JsonResponse.success(admin);
}
/**
* 描述:根据Id删除
*
*/
@ApiOperation(value="删除用户", notes="根据url的id来删除指定对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseBody
public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
adminService.removeById(id);
return JsonResponse.success(null);
}
/**
* 描述:根据Id 更新
*
*/
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体admin", required = true, dataType = "Admin")
})
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ResponseBody
public JsonResponse updateAdmin(@PathVariable("id") Long id,Admin admin) throws Exception {
admin.setId(id);
adminService.updateById(admin);
return JsonResponse.success(null);
}
/**
* 描述:创建Admin
*
*/
@ApiOperation(value="创建用户", notes="根据admin对象创建用户")
@ApiImplicitParam(name = "admin", value = "用户详细实体user", required = true, dataType = "Admin")
@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
public JsonResponse create(Admin admin) throws Exception {
adminService.save(admin);
return JsonResponse.success(null);
}
}
swagger2常用属性:
运行项目,输入网址:
输入网址:http://localhost:8887/swagger-ui.html
参考链接:https://blog.didispace.com/springbootswagger2/