前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swagger UI

Swagger UI

作者头像
疯狂的KK
修改2020-10-12 09:35:00
1.2K0
修改2020-10-12 09:35:00
举报
文章被收录于专栏:Java项目实战Java项目实战

在前后端分离并行开发时,当定完需求文档,需要根据接口文档进行接口对接,如果接口文档后置进行,对完成的接口进行参数输出输出也能棘手,毕竟可以进行测试,打印参数,几遍是这样,使用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.*;
@Test
public 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

#生产环境需要关闭 swagger 防止接口暴露

1,启动判断写在相应的环境配置文件中,根据条件判断是否启动 swagger : 添加配置项:swagger.is.enable

#是否激活 swagger true or false
swagger.is.enable=true
12

2,代码取值,设置是否加载 swagger:

@Value("${swagger.is.enable}")
private boolean swagger_is_enable;

@Bean
public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)
    .enable(swagger_is_enable)
    .apiInfo(apiInfo()).select()
    // 扫描指定包中的swagger注解
    .apis(RequestHandlerSelectors.basePackage("springboot_druid_demo.controller"))
    .paths(PathSelectors.any())
    .build()
    .pathMapping("/");
}
123456789101112131415
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 赵KK日常技术记录 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Springfox ,找到Springfox Swagger2,以及Springfox Swagger UI。
  • Getting Started
    • Installation
      • Maven
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档