"丝袜哥"REST API
在前后端分离并行开发时,当定完需求文档,需要根据接口文档进行接口对接,如果接口文档后置进行,对完成的接口进行参数输出输出也能棘手,毕竟可以进行测试,打印参数,即便是这样,使用Yapi的时候也需要手动或导入Json的形式书写,如果接口发生变动,还需要随之改变接口文档,学习下swagger API生成文档。
pom引入依赖,https://mvnrepository.com/,maven仓库搜索
Springfox ,找到Springfox Swagger2,以及Springfox Swagger UI。
<!--swaggerAPI--> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.10.5</version> </dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.10.5</version> </dependency>
我哭了,Springboot2.2.2.RELEASE使用注解时,需搭配2.7.0
主启动类引入@EnableSwagger2注解
Controller
@PostMapping("/payment/getuserinfo") public List<User> getUserInfo(@RequestBody User user){
List<User> list = new ArrayList<>(); list.add(User .builder() .userName("kk") .password("123456") .mobile("13800000000") .build()); list.add(User .builder() .userName("bobo") .password("1234567") .mobile("13700000000") .build()); list.add(User .builder() .userName("hh") .password("12345678") .mobile("13700000000") .build()); list.add(User .builder() .userName("zz") .password("123456789") .mobile("13700000000") .build()); list.removeIf(users->"123456".equals(users.getPassword())); return list; }
http://localhost:8080/swagger-ui.html
Swagger使用的注解及其说明:
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
l code:数字,例如400
l message:信息,例如"请求参数没填好"
l response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
l @ApiModelProperty:描述一个model的属性
@ApiImplicitParam(paramType="query", name = "username", value = "用户名", required = false, dataType = "String")
如果一个参数一个参数的利用@RequestParam也可以,推荐在实体类上加注解
@Data
@Builder
public class TsysUser implements Serializable {
private String id;
@ApiModelProperty(value="用户名")
private String username;
private String password;
private String nickname;
private static final long serialVersionUID = 1L;
public TsysUser(String id, String username, String password, String nickname) {
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
}
重启再次访问
相比较之下就多了字段注释
方法注解@ApiOperation(value="用户查询服务",notes="首页/用户管理")
@PostMapping("/payment/getuserinfo") @ApiImplicitParam(paramType="query", name = "username", value = "用户名", required = false, dataType = "String") --> @ApiOperation(value="用户查询服务",notes="首页/用户管理") @ResponseBody public List<TsysUser> getUserInfo(@RequestBody TsysUser user){
Try it out 模拟请求
WireMock REST FUL伪造服务
官网:http://wiremock.org/docs/running-standalone/
通过jar包形式启动
Getting Started
Installation
WireMock is distributed via Maven Central and can be included in your project using common build tools’ dependency management.
To add the standard WireMock JAR as a project dependency, put the following in the dependencies section of your build file:
Maven
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-jre8</artifactId> <version>2.27.0</version> <scope>test</scope></dependency>
测试
To use WireMock’s fluent API add the following import:
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@Testpublic void exampleTest() { stubFor(get(urlEqualTo("/my/resource")) .withHeader("Accept", equalTo("text/xml")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "text/xml") .withBody("<response>Some content</response>")));
Result result = myHttpServiceCallingObject.doSomething();
assertTrue(result.wasSuccessful());
verify(postRequestedFor(urlMatching("/my/resource/[a-z0-9]+")) .withRequestBody(matching(".*<message>1234</message>.*")) .withHeader("Content-Type", notMatching("application/json")));}
详细使用阅读了https://www.jianshu.com/p/481b04e13ba9
这个确实方便,就是不知道前端同学喜不喜欢这样对接,平日的开发都是张嘴就要Yapi的,毕竟Yapi写好了,如果出现锅比较好甩
- 点赞
- 收藏
- 关注作者
评论(0)