javaee springboot-swagger
swagger-1.7 & swagger-2.2 使用方法
一、swagger-1.7
1.pom引用类库
<!-- swagger -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
2.启动程序添加注解 @EnableSwagger2Doc
@EnableSwagger2Doc
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.控制器添加注解 @Api @ApiOperation
@Controller
@RequestMapping("/swagger")
@Api(value = "没有tags,value作为分组名称", tags = {"分组名称"}, description = "详细类描述")
public class SwaggerController {
//http://127.0.0.1:8080/swagger-ui.html
@RequestMapping("/test")
@ResponseBody
@ApiOperation(value="方法描述", notes="详细方法描述")
public Result test(Category category) {
return Result.suc();
}
}
4.实体类添加注解 @ApiModel @ApiModelProperty
@Data
@ApiModel(value = "Category别名", description = "详细类描述")
public class Category {
@ApiModelProperty(value = "id描述", example = "1")
private int id;
@ApiModelProperty(value = "name描述", example = "name1")
private String name;
}
5.请求文档地址
http://127.0.0.1:8080/swagger-ui.html
二、swagger-2.2
1.pom引用类库
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2.配置类
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api() {
return docket("api","com.demo.springboot.controller");
}
Docket docket(String groupName, String basePackage){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName(groupName)
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("demo系统")
.description("demo系统描述")
.termsOfServiceUrl("http://127.0.0.1:8080")
.version("1.0")
.build();
}
}
3.控制器添加注解 @Api @ApiOperation 【与swagger-1.7一样】
@Controller
@RequestMapping("/swagger")
@Api(value = "没有tags,value作为分组名称", tags = {"分组名称"}, description = "详细类描述")
public class SwaggerController {
//http://127.0.0.1:8080/swagger-ui.html
@RequestMapping("/test")
@ResponseBody
@ApiOperation(value="方法描述", notes="详细方法描述")
public Result test(@RequestBody Category category) {
return Result.suc();
}
}
4.实体类添加注解 @ApiModel @ApiModelProperty 【与swagger-1.7一样】
@Data
@ApiModel(value = "Category别名", description = "详细类描述")
public class Category {
@ApiModelProperty(value = "id描述", example = "1")
private int id;
@ApiModelProperty(value = "name描述", example = "name1")
private String name;
}
5.请求文档地址 【与swagger-1.7一样】
http://127.0.0.1:8080/swagger-ui.html

浙公网安备 33010602011771号