Swagger結合springboot使用

1.環境


SpringBoot    2.0.4.RELEASE
Swagger    1.7.1
JDK    1.8


2.依賴

<dependency>
	<groupId>com.spring4all</groupId>
	<artifactId>swagger-spring-boot-starter</artifactId>
	<version>1.7.1.RELEASE</version>
</dependency>


3.Swagger配置Bean
@Configuration會被項目啓動加載爲配置類,等同於XML中配置的beans;
@Bean標註方法等同於XML中配置的bean。

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xx.xx.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xx系統")
                .description("xxx模塊")
                .contact("xxxxxx")
                .version("1.0")
                .build();
    }

啓動類中加入@EnableSwagger2 代表swagger開啓:

@EnableScheduling
@SpringBootApplication
@EnableSwagger2
@EnableCaching
@ComponentScan(basePackages={"com.xx"})
@EnableJpaRepositories
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


4.定義restful接口

@RestController
@RequestMapping("web/address")
@Api(value="地址管理模塊")
public class AddressController {
	@Autowired
	IAddressService addressService;

	@BussinessLog("獲取地區")
	@RequestMapping(value = "getList")
	@ApiOperation(value="獲取地區",notes="根據id獲取地址列表" produces = "application/json",httpMethod="GET")
	public ResponseResult<List<APIAddressResponse>> list(Integer pid) {
		 return ResponseResult.SUCCESS(apiAddressTree);
	}
	
}


6.啓動


啓動項目訪問 http://localhost:8080/swagger-ui.html

這個時候,swagger的ui就展現到了你的面前,和上面的圖一樣

 

7.Swagger常用註解

用於類

- @Api()  表示標識這個類是swagger的資源 

- @ApiModel() 對類進行說明,用於參數用實體類接收 

- @ApiIgnore()用於類,方法,方法參數 表示這個方法或者類被忽略 

用於方法:
- @ApiOperation()  表示一個http請求的操作 

- @ApiImplicitParam()  表示單獨的請求參數 

- @ApiImplicitParams() 包含多個 @ApiImplicitParam

用戶多處:
- @ApiParam()用於方法,參數,字段說明; 表示對參數的添加元數據(說明或是否必填等) 

- @ApiModelProperty()用於方法,字段 表示對model屬性的說明或者數據操作更改 
 

具體使用舉例說明: 
@Api() 
用於類;表示標識這個類是swagger的資源 
tags–表示說明 
value–也是說明,可以使用tags替代 
但是tags如果有多個值,會生成多個list

一張圖說明所有問題
 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章