这种REST API风格很不错!看看效果!
这个rest api接口文档后,比普通文档很实用!
好了,开始正题,实现!—— 首先有个maven spring boot工程基础,spring boot工程配置可以参考博客上几篇。
第一步:在pom.xml中导入swagger2依赖包
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<springfox-swagger2.version>2.5.0</springfox-swagger2.version>
<fastjson.version>1.2.12</fastjson.version>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>
<!-- http://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>
导入完毕,这时候 update一下maven工程
第二部:新建Rest API 文档相关配置java类 SwaggerConfig.java
package com.mts.mybatisgenerator.generator.api.rest.docs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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;
@Configuration
@EnableSwagger2
@ComponentScan("com.mts.mybatisgenerator.generator.api.rest")
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mts.mybatisgenerator.generator.api.rest"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder().title("Spring Boot Sample REST APIs")
.description("The APIs here demonstrate creating a service built with Spring Boot")
.license("MIT")
.licenseUrl("http://opensource.org/licenses/MIT")
.contact(new Contact("GuangLin", "OuYang", "93775988@qq.com"))
.version("1.0")
.build();
return apiInfo;
}
}
上面类中这个路径不要写错了,很重要!
com.mts.mybatisgenerator.generator.api.rest
第三步:建立一个control类 HomeController.java —— 注意也是在上面的rest包下面
package com.mts.mybatisgenerator.generator.api.rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.mts.mybatisgenerator.generator.model.TMsmartPayment;
@RestController
@RequestMapping(value="/controll/v1/api")
@Api(value="GeneratorAPI",description="GeneratorAPI LIST")
public class HomeController {
@RequestMapping(value="add",method=RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value="",notes="")
public String add(){
return "success";
}
@RequestMapping(value="query",method=RequestMethod.POST,produces={"application/json","application/xml"})
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value="",notes="")
public String query(@ApiParam(name="id",value="查询参数用户ID")String id,@ApiParam(name="name",value="查询参数用户名")String name){
return "success";
}
@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json","application/xml"})
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value="",notes="")
public String update(@RequestBody TMsmartPayment tmsPayment,HttpServletRequest request,HttpServletResponse response){
return "success";
}
@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json","application/xml"})
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value="",notes="")
public String delete(@RequestBody TMsmartPayment tmsPayment,HttpServletRequest request,HttpServletResponse response){
return "success";
}
}
到这步就完成了!
第四步:启动Application.java
第五步:
访问swagger的默认首页地址: http://localhost:8080/swagger-ui.html
Try it out!结果:
成功发送
query方法是带参数文档描述:
使用方式:
public String query(@ApiParam(name="id",value="查询参数用户ID")String id,@ApiParam(name="name",value="查询参数用户名")String name){
接下来继续研究,Swagger的接口代码生成功能~