Swagger
目录
个人感觉Swagger比Postman更加的直观而方便的进行调试
下面我们先预览一下最基本的功能
可以直观的看到项目中的api,一级各个api的功能以及请求方式,比手搓api文档好多了。。
当然也可以进行各个接口的测试:
进行简单的测试,查看数据是否传输正确,非常方便。
下面我们来看看怎么将swagger整合到ssm中吧
一 maven中添加依赖
<!--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>
二 配置swagger
简单的配置,目录结构
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true) // 是否禁用swagger
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger API文档")
.description("Dcpnet商品管理系统接口文档")
.version("1.0")
.build();
}
}
三 spring-mvc中进行自动扫包
<context:component-scan base-package="com.dcpnet.swagger"/>
四 添加相关注释
首先再Controller外层添加@Api的注解,然后再内部使用@ApiOperation的注解,在里面进行配置,更多的配置请到官网查看
@RestController
@RequestMapping("/user")
@Api(value = "UserController管理",tags = "UserController管理接口API")
public class UserController {
/**
* 服务对象
*/
@Resource
private UserService userService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("/chaXunById")
@ApiOperation(value = "通过id查询对象接口",notes = "通过id查询对象接口",httpMethod = "get")
public User chaXunById(Integer id) {
return this.userService.chaXunById(id);
}
/**
* 通过主键删除单条数据
*
* @param id 主键
* @return Map
*/
@RequestMapping(value = "/shanChuById",method = RequestMethod.POST)
@ApiOperation(value = "通过id删除对象接口",notes = "通过id删除对象接口",httpMethod = "post")
public Map<String,Object> shanChuById(@Param("id")Integer id){
return this.userService.shanChuById(id);
}
}
我发布过一个SSM模板项目(脚手架项目),里面有配置好的swagger,以及editor.md,可以直接使用。谢谢
资源链接 脚手架项目