What is swagger?
It is known as the most popular Api framework in the world,
ReatFul Api document online generation tool=》 Api documents and Api definitions are updated synchronously and
run directly, and Api interfaces can be tested online
Support multiple languages
official website
https://swagger.io/
How to use Swagger to
import dependencies in a project
<!--swagger 依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
First click on a Spring boot project
Simple hello program
package com.jj.demo.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello,swagger!";
}
}
Configure the configuration file of Swagger.
You can access the page of swagger by simply configuring it.
package com.jj.demo.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
//加入Swagger 的注解 ,开启
@EnableSwagger2
public class Swaggerconfig {
}
Visit http://localhost:8080/swagger-ui.html
how to modify it to your own name
package com.jj.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
//加入Swagger 的注解 ,开启
@EnableSwagger2
public class Swaggerconfig {
// 配置Swagger 的 Docket 的bean 实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
// 配置Swagger 信息
private ApiInfo apiInfo(){
// 作者信息
Contact contact = new Contact("娇娇", "", "[email protected]");
return new ApiInfo(
"娇娇的Api 文档","生而为人,务必善良","1.0","",contact,"","",new ArrayList<>()
);
}
}
Effect
**Accessories Swagger Scan**
// 配置Swagger 的 Docket 的bean 实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// RequestHandlerSelectors 配置要扫描接口的方式
// basePackage :指定要扫描的包
// any() :扫描全部
// none():不扫描
// withClassAnnotation //扫描类上的注解,参数市一个注解的反射对象
// withMethodAnnotation // 扫描方法上的注解
// 。paths()过滤什么路径
.apis(RequestHandlerSelectors.basePackage("com.jj.demo.Controller"))
.build()
;
}
Turn off the switch and you
will see this cute emoji pack! !
The question is, how do I only want my Swagger to be used in the production environment and not to use it when it is released?
Determine whether the production environment flag=false
inject enable (flag). The
first step
is to send the required configuration file in the config class, and put the parameters first.
Then call the method and put the test environment in it
. Put the obtained boolean value in the open swagger, because it just needs a boolean type.
Effect
At the moment, the environment we open is the dev configuration file
spring.profiles.active=dev
Start the project and
see
how the flag is true at the moment, how to configure grouping, and how to configure multiple grouping
effects,
how to configure multiple groups,
Several commonly used annotation
entity classes are public, I made a mistake! ! ! ! ! ! ! ! ! ! 11
effect
** ApiOperation notes and comments ApiParam **
// ApiOperation 接口,不是放在类上的,是方法上
@ApiOperation("hello 的第二个控制类")
@RequestMapping("/hello2")
// 参数的文档注释
public String hello2(@ApiParam("用户名") String name){
return "hello"+name;
}
effect