本文测试代码使用 Spring Boot 2.1.6.RELEASE + Swagger 2.9.2
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
swagger:
enable: true
@Configuration
@EnableSwagger2 // 启用 Swagger
@ConditionalOnExpression("${swagger.enable:true}") // 根据配置决定是否最终启用 Swagger
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.karonda.springbootswagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SWAGGER 测试平台")
.description("测试 SWAGGER 用")
.termsOfServiceUrl("https://github.com/VictorBu")
.version("1.0.0")
.build();
}
}
@RestController
@Api(tags = "测试接口")
public class HiController {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
@ApiOperation(value="打招呼")
@ApiImplicitParams({
@ApiImplicitParam(name = "firstName", value = "名字", paramType = "query", required = true),
@ApiImplicitParam(name = "lastName", value = "姓氏", paramType = "query", required = true)
})
public String sayHi(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName){
return "Hi, " + firstName + " " + lastName;
}
}
启动程序输入 http://localhost:8080/swagger-ui.html 即可看到效果
完整代码:GitHub
原文:https://www.cnblogs.com/victorbu/p/11094359.html