Separating the front and rear ends, a swagger2 speed development and WireMock

First, the interface documentation is generated using swagger2

  1. rely

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
    </dependency>
    <-! Bootstrap the swagger of landscaping Interface ->
    <dependency> 
      <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
    </dependency>
  2. On startup class springboot plus @ EnableSwagger2 comment
  3. On the controller interface plus @ApiOperation (value = "user query service") annotation indicates that the role of the interface.

swagger2 commonly used annotation:

@Api (): used in the request class diagram illustrating the class, but also represents resources of this class is swagger2

   参数:tags:说明该类的作用,参数是个数组,可以填多个。
      value="该参数没什么意义,在UI界面上不显示,所以不用配置"
      description = "用户基本信息操作"

A method for the operation, represents a method to access the http request: @ApiOperation ()

参数:value="方法的用途和作用"    
   notes="方法的注意事项和备注"    
   tags:说明该方法的作用,参数是个数组,可以填多个。
   格式:tags={"作用1","作用2"} 
   (在这里建议不使用这个参数,会使界面看上去有点乱,前两个常用)

@ApiModel (): the response to the entity class, the entity for explaining the effect of

参数:description="描述实体的作用"  

@ApiModelProperty: properties used in the description of the entity class attributes

参数:value="用户名"  描述参数的意义
   name="name"    参数的变量名
   required=true     参数是否必选

@ApiImplicitParams: a request used in the method, comprising a plurality @ApiImplicitParam

@ApiImplicitParam: a method, which represents a separate request parameters

参数:name="参数ming" 
   value="参数说明"
   dataType="数据类型"
   paramType="query" 表示参数放在哪里
    · header 请求参数的获取:@RequestHeader
    · query   请求参数的获取:@RequestParam
    · path(用于restful接口) 请求参数的获取:@PathVariable
    · body(不常用)
    · form(不常用) 
defaultValue="参数的默认值"
required="true" 表示参数是否必须传

@ApiParam (): method for the parameter, the parameter field specifies requirements for showing and description

参数:name="参数名称"
   value="参数的简要说明"
   defaultValue="参数默认值"
   required="true" 表示属性是否必填,默认为false

@ApiResponses: a method for requesting, according to a different response code indicates the response, a @ApiResponses comprising a plurality @ApiResponse

@ApiResponse: used in the method of the request, the response indicates a different

参数:code="404"    表示响应码(int型),可自定义
   message="状态码对应的响应信息"   

@ApiIgnore (): on a class or method, is not displayed on the page
@Profile ({ "dev", " test"}): for the configuration class, represents only useful for development and testing environment

 

Second, the counterfeit REST services using WireMock

WireMock is a flexible library for Web services testing, and other testing tools are different, WireMock create an actual HTTP server to run your Web services to facilitate testing.

It supports the stub HTTP response, requests authentication, the proxy / interception, recording and playback, and can be used to deploy a test environment or in the test unit.

scenes to be used:

  • Testing mobile applications rely on third-party REST APIs
  • Create APIs for rapid prototyping
  • Otherwise difficult to simulate the injection of third-party service error
  • Any code that depends on the unit test web service
  1. Official website: http://wiremock.org/

    Download an executable jar: http: //wiremock.org/docs/running-standalone/

  2. java-jar启动jar包   java -jar wiremock-standalone-2.18.0.jar --port 3298 
  3. Add dependent
    <dependency>
          <groupId>com.github.tomakehurst</groupId>
          <artifactId>wiremock</artifactId>
          <version>1.53</version>
          <classifier>standalone</classifier>
    </dependency>
      <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
      </dependency>
  4. Create documents in the folder resources to establish txt file, storing the returned json data
  5. Add Setting
    public static void main(String[] args) throws IOException {
            WireMock.configureFor("127.0.0.1",3298);//设置路径和端口
            WireMock. removeAllMappings();//清空所有设置
    
            mock("/test/01", "01");
            mock("/test/02", "02");
        }
    
        private static void mock(String url, String file) throws IOException {
            ClassPathResource resource = new ClassPathResource("response/" + file + ".txt");
            String content StringUtils.join = (FileUtils.readLines (resource.getFile (), "UTF-. 8") toArray (), "\ n-."); // return result 
            stubFor (get (urlPathEqualTo (url) ) willReturn (aResponse (. ) .withBody (Content) .withStatus (200 is))); // GET or post requests return value 
        }

     

     

Guess you like

Origin www.cnblogs.com/dinghaoran/p/11876525.html