依赖
< ! -- swagger 文档注释-- >
< 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>
< ! -- swagger-- >
Swagger的Configuration
扫描指定接口
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket ( ) {
return new Docket ( DocumentationType. SWAGGER_2)
. select ( )
. apis ( RequestHandlerSelectors. basePackage ( "hello.spring.cloud.service.controller" ) )
. build ( ) ;
}
}
扫描指定请求
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket ( ) {
return new Docket ( DocumentationType. SWAGGER_2)
. select ( )
. apis ( RequestHandlerSelectors. basePackage ( "hello.spring.cloud.service.controller" ) )
. paths ( PathSelectors. ant ( "/user/**" ) )
. build ( ) ;
}
}
指定忽略的接口参数
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket ( ) {
return new Docket ( DocumentationType. SWAGGER_2)
. ignoredParameterTypes ( HttpSession. class ) ;
}
}
指定swagger在哪种环境下可以展示
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
Environment environment;
@Bean
public Docket docket ( ) {
Profiles profiles = Profiles. of ( "dev" , "test" ) ;
boolean isEnable = environment. acceptsProfiles ( profiles) ;
return new Docket ( DocumentationType. SWAGGER_2)
. enable ( isEnable) ;
}
}
spring:
profiles:
active: pro
API进行分组展示
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
Environment environment;
@Bean
public Docket docketUser ( ) {
return new Docket ( DocumentationType. SWAGGER_2)
. groupName ( "用户" )
. select ( )
. paths ( PathSelectors. ant ( "/user/**" ) )
. build ( ) ;
}
@Bean
public Docket docketTest ( ) {
return new Docket ( DocumentationType. SWAGGER_2)
. groupName ( "测试" )
. select ( )
. paths ( PathSelectors. ant ( "/test/**" ) )
. build ( ) ;
}
}
实体信息配置
@ApiModel ( "用户实体" )
public class User {
@ApiModelProperty ( "用户名" )
String name;
@ApiModelProperty ( "用户密码" )
String password;
@ApiModelProperty ( "年龄" )
Integer age;
全局参数配置
@Configuration
@EnableSwagger2
public class SwaggerConfig { ;
@Bean
public Docket docket ( ) {
Parameter build = new ParameterBuilder ( ) . name ( "token" )
. description ( "用户登录令牌" )
. parameterType ( "header" )
. modelRef ( new ModelRef ( "String" ) )
. required ( true )
. build ( ) ;
List< Parameter> parameters = new ArrayList < > ( ) ;
parameters. add ( build) ;
return new Docket ( DocumentationType. SWAGGER_2)
. globalOperationParameters ( parameters) ;
}
}
描述接口信息
@Api ( tags = "用户相关的请求" )
@RestController
@RequestMapping ( "/user" )
public class SwaggerTestController {
@ApiOperation ( "获取用户信息" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "name" , value = "用户名" ) ,
@ApiImplicitParam ( name = "password" , value = "用户密码" )
} )
@GetMapping ( "/getUser" )
public User getUser ( String name, String password) {
User user = new User ( "张三" , "123456" , 18 ) ;
return user;
}
@PostMapping ( "/setUser" )
public String setUser ( @RequestBody User user) {
return user. getName ( ) ;
}
}