If your company hasn't used swagger or even heard of swagger, hurry up and learn this blog, five minutes quick, fool-style integration, but such a simple application will surely shock them.
First, let’s give a brief introduction to swagger: swagger is a magical tool for back-end development and a channel for front-end communication. What can you do with swagger? First of all, you can basically say goodbye to unit testing in the future; secondly, you no longer have to write interface documents, and you don’t need to maintain the documents after writing them. Swagger can fully simulate http requests, and the difference between incoming and outgoing parameters and actual conditions is almost zero. Having said this, let’s just make some information!
Integrated four-part:
Step 1: Import two dependencies. If you are not a maven project, then you can look for the jar package. Remember that only two are needed. I saw that I introduced seven or eight of them in other tutorials, which is a waste.
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version></dependency>
Step 2: Add a class (copy the following one, please note that you can modify the package name and address)
/** * Swagger configuration* * @author wq * @since 2017-05-16 */@EnableWebMvc@EnableSwagger2@Configurationpublic class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.z*.b*.c*.controller")) // Note that you modify the package name here.paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Interface List v1.1.0") // Any, please regulate it a little.description("Interface Test") // Any, please regulate it a little.termsOfServiceUrl("http://url/swagger-ui.html") // Change "url" to your own ip:port .contact("laowu") // It doesn't matter (here is the author's alias) .version("1.1.0") .build(); }} Step 3: Add the following configuration to the mvc configuration file. Maybe your file may be called dispatcher.xml! (Just copy, no modification is required)
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>><mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Step 4: Add annotations on methods and parameters
Method:
@ApiOperation(value = "tutorial", httpMethod = "POST", notes = "tutorial")
Put it in the registry:
@ApiParam(required = true, name = "test", value = "Tutorial parameter")
I’m worried that some friends don’t quite understand, so let’s put a picture!
Step 5: Start the service and enter in the browser:
http://ip:port/swagger-ui.html
The following screen appears means the task is completed:
Note: If you use an interceptor in your project, please release the swagger resource (you can still copy the following configuration directly, don't doubt v2)
<mvc:exclude-mapping path="/swagger*/**"></mvc:exclude-mapping><mvc:exclude-mapping path="/v2/**"></mvc:exclude-mapping><mvc:exclude-mapping path="/webjars/**"></mvc:exclude-mapping>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.