SpringBoot18 Swagger, API interface document generation, Wiremock, simulation background data

Source: Internet
Author: User

1 Swagger 1.1 Description

The front-end separation of the project needs the front-end developers to work together, the background developers need to give the front-end developer a set of API documentation, using swagger can easily and efficiently help background developers to generate RESTFULAPI development documents

Website address: Click to go

1.2 Using step 1.2.1 to introduce swagger dependent dependencies
        <!--automatically generate RESTFULAPI document-related -        <Dependency>            <groupId>Io.springfox</groupId>            <Artifactid>Springfox-swagger2</Artifactid>            <version>2.7.0</version>        </Dependency>        <Dependency>            <groupId>Io.springfox</groupId>            <Artifactid>Springfox-swagger-ui</Artifactid>            <version>2.7.0</version>        </Dependency>
1.2.3 Support Annotations

Labeling @EnableSwagger2 on the startup class of the Springboot project to enable the project to support swagger annotations

1.2.4 Documentation Annotations

Annotate the appropriate annotations where you need to generate the API documentation

"Method level

        @ApiOperation (value = " query all user Information ")

          

"Parameter level (single parameter)

        @ApiParam (value = " order ID")   

"Parameter level (parameter is an entity class)

        @ApiModelProperty (value = " user ID")

Tip 01: Add @ApiModelProperty directly to a field in the Entity class (value = " user ID")

          

1.2.5 Access swagger-ui.html

Tip 01: If the project sets the context path, then you need to add the context path in front of it, for example

        

Http://127.0.0.1:9999/dev/swagger-ui.html

      

2 Wiremock

Front-end developers need some acquired simulation data, and background developers can use Wiremock to simulate some data for front-end people to call

Tip 01:wiremock is a separate server

2.1 Using Wiremock alone

Directly put the data into the specified file, after some configuration and then start the Wiremock server can be

Tip 01: This method is simple to use but not very convenient for background developers, please refer to Baidu for detailed usage methods.

Tip # 02: This method is suitable for front-end personnel (PS: the front-end person gets the JSON file back to him)

2.2 Integration of Wiremock 2.2.1 Download Wiremock jar package in Project

Download the jar package locally to Wiremock's website

Wiermock Official website: Click to go

      

2.2.2 Start Wiermock

The downloaded Wiremock is equivalent to the jar package for a project, and we only need to run the jar package on the JVM to

Tip 01: Go to the folder where Wiremock's jar package is located and run the jar package

Tip 02: You can specify ports and other information when running this jar package, see the official documentation for details

Java-jar Wiremock-standalone-2.17.0.jar--port=8062

      

2.2.3 Integration in the Springboot project (simple)

2.2.3.1 download First off jar package

        <Dependency>            <groupId>Com.github.tomakehurst</groupId>            <Artifactid>Wiremock</Artifactid>            <version>2.14.0</version>        </Dependency>

2.2.3.2 Publishing Information

"Connection Configuration

Tip 01: You need to configure the IP address and port of the Wiremock, if it is local do not need to configure the IP address, directly configure the port

"Clears the last release message.

"Executes the Main method for message publishing

 PackageCom.example.wiremock.util;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.lang3.StringUtils;ImportOrg.springframework.core.io.ClassPathResource;Importjava.io.IOException;Import Staticcom.github.tomakehurst.wiremock.client.wiremock.*;/** * @authorWang Yangji * @create 2018-05-11 9:29 * @desc **/ Public classWireMockServerUtil02 { Public Static voidMain (string[] args)throwsIOException {//01 Connection ConfigurationConfigurefor (8062);//Configure connection information (PS: This port must be consistent with the port on which the Wiremock is started)//02 emptying the release informationRemoveallmappings ();//empty the last release information//03 release new informationstubfor (Get (Urlpathequalto ("/wiremock/test"))//Set Request Path. Willreturn (Aresponse ()//Setting Response Information. withbody ("{\" id\ ": 12,\" name\ ": Null,\" password\ ": null}")//Response Data. Withstatus (200)//Response Status Code                        )        ); }}
Wiremockserverutil02.java

2.2.3.3 request simulation data in Wiremock

Tip 01:ip address, port, request path are wiremock, not springboot project

        

2.2.4 Integration in the Springboot Project (practical)

Put the data you want to publish into a TXT file, you need to publish the data in a TXT file, call a method directly

2.2.4.1 Download Related jar packages

        <Dependency>            <groupId>Com.github.tomakehurst</groupId>            <Artifactid>Wiremock</Artifactid>            <version>2.14.0</version>        </Dependency>        <Dependency>            <groupId>Commons-io</groupId>            <Artifactid>Commons-io</Artifactid>            <version>2.4</version>        </Dependency>

2.2.4.2 Publishing Information

"Create a folder under the Resources directory

          

Create a TXT file inside to store the background simulation data you need to publish (PS: Data to be written in JSON format)

Tip 01: A file can hold only one request corresponding to the background simulation data

{    "name": "Wang Yangji",    "age": +,    "address": "Chongqingyuzu"    "gender": "F"}
User.txt

"Tool class

"Wiremock Connection Information configuration

"Clears the release information

"Publish Tool method

     Public Static void throws IOException {        new classpathresource ("mock/response/" + filename);         = Stringutils.join (Fileutils.readlines (Classpathresource.getfile (), "UTF-8"). ToArray (), "\ n");        Stubfor (                get (Urlpathequalto (URL))                        . Willreturn (                                aresponse ().                                withbody (data)                                . (Withstatus))        ;    }
Publish Method

"Call publish method publish information

Tip 01: Just pass in the file name and request path

 PackageCom.example.wiremock.util;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.lang3.StringUtils;ImportOrg.springframework.core.io.ClassPathResource;Importjava.io.IOException;Import Staticcom.github.tomakehurst.wiremock.client.wiremock.*;/** * @authorWang Yangji * @create 2018-05-11 9:29 * @desc **/ Public classWiremockserverutil { Public Static voidMain (string[] args)throwsIOException {configurefor (8062);        Removeallmappings (); Mock ("User.txt", "/user"); Mock ("Teacher.txt", "/teacher"); }     Public Static voidMock (string filename, string url)throwsIOException {classpathresource Classpathresource=NewClasspathresource ("mock/response/" +filename); String Data= Stringutils.join (Fileutils.readlines (Classpathresource.getfile (), "UTF-8"). ToArray (), "\ n"); Stubfor (Get (Urlpathequalto (URL)). Willreturn (Arespo NSE (). Withbody (data). Withstatus (200)                        )        ); }}
Wiremockserverutil.java

"Executes the Main method for message publishing

2.2.4.3 request simulation data in Wiremock

        

SpringBoot18 Swagger, API interface document generation, Wiremock, simulation background data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.