[TOC]
# 依賴
~~~
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
~~~
# 常用注解
~~~
Api:修飾整個類,描述Controller的作用
ApiOperation:描述一個類的一個方法,或者說一個接口
ApiParam:單個參數描述
ApiModel:用對象來接收參數
ApiProperty:用對象接收參數時,描述對象的一個字段
ApiResponse:HTTP響應其中1個描述
ApiResponses:HTTP響應整體描述
ApiIgnore:使用該注解忽略這個API
ApiError :發生錯誤返回的信息
ApiImplicitParam:一個請求參數
ApiImplicitParams:多個請求參數
~~~
## Api標記
默認情況,只會掃描具有@Api注解的類
Api 用在類上,說明該類的作用。可以標記一個Controller類做為swagger 文檔資源,使用方式:
~~~
@Api(value = "/user", description = "Operations about user")
~~~
與Controller注解并列使用。 屬性配置:
| 屬性名稱 | 備注 |
| --- | --- |
| value | url的路徑值 |
| tags | 如果設置這個值、value的值會被覆蓋 |
| description | 對api資源的描述 |
| basePath | 基本路徑可以不配置 |
| position | 如果配置多個Api 想改變顯示的順序位置 |
| produces | For example, "application/json, application/xml" |
| consumes | For example, "application/json, application/xml" |
| protocols | Possible values: http, https, ws, wss. |
| authorizations | 高級特性認證時配置 |
| hidden | 配置為true 將在文檔中隱藏 |
在SpringMvc中的配置如下:
~~~
@Controller
@RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
@Api(value = "/pet", description = "Operations about pets")
public class PetController {
}
~~~
~~~
@Api(value = "消息" ,description = "用戶接口", protocols = "http")
@RestController
@RequestMapping("user")
public class UserController {
~~~

## ApiOperation標記
ApiOperation:用在方法上,說明方法的作用,每一個url資源的定義,使用方式:
~~~
@ApiOperation(
value = "Find purchase order by ID",
notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
response = Order,
tags = {"Pet Store"})
~~~
與Controller中的方法并列使用。
屬性配置:
| 屬性名稱 | 備注 |
| --- | --- |
| value | url的路徑值 |
| tags | 如果設置這個值、value的值會被覆蓋 |
| description | 對api資源的描述 |
| basePath | 基本路徑可以不配置 |
| position | 如果配置多個Api 想改變顯示的順序位置 |
| produces | For example, "application/json, application/xml" |
| consumes | For example, "application/json, application/xml" |
| protocols | Possible values: http, https, ws, wss. |
| authorizations | 高級特性認證時配置 |
| hidden | 配置為true 將在文檔中隱藏 |
| response | 返回的對象 |
| responseContainer | 這些對象是有效的 "List", "Set" or "Map".,其他無效 |
| httpMethod | "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH" |
| code | http的狀態碼 默認 200 |
| extensions | 擴展屬性 |
在SpringMvc中的配置如下:
~~~kotlin
@RequestMapping(value = "/order/{orderId}", method = GET)
@ApiOperation(
value = "Find purchase order by ID",
notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
response = Order.class,
tags = { "Pet Store" })
public ResponseEntity<Order> getOrderById(@PathVariable("orderId") String orderId)
throws NotFoundException {
Order order = storeData.get(Long.valueOf(orderId));
if (null != order) {
return ok(order);
} else {
throw new NotFoundException(404, "Order not found");
}
}
~~~
## ApiParam標記
ApiParam請求屬性,使用方式
~~~kotlin
public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true) User user)
~~~
與Controller中的方法并列使用。
屬性配置:
| 屬性名稱 | 備注 |
| --- | --- |
| name | 屬性名稱 |
| value | 屬性值 |
| defaultValue | 默認屬性值 |
| allowableValues | 可以不配置 |
| required | 是否屬性必填 |
| access | 不過多描述 |
| allowMultiple | 默認為false |
| hidden | 隱藏該屬性 |
| example | 舉例子 |
在SpringMvc中的配置如下:
~~~kotlin
public ResponseEntity<Order> getOrderById(
@ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)
@PathVariable("orderId") String orderId)
~~~
## ApiResponse
ApiResponse:響應配置,使用方式:
~~~
@ApiResponse(code = 400, message = "Invalid user supplied")
~~~
與Controller中的方法并列使用。 屬性配置:
| 屬性名稱 | 備注 |
| --- | --- |
| code | http的狀態碼 |
| message | 描述 |
| response | 默認響應類 Void |
| reference | 參考ApiOperation中配置 |
| responseHeaders | 參考 ResponseHeader 屬性配置說明 |
| responseContainer | 參考ApiOperation中配置 |
在SpringMvc中的配置如下:
~~~kotlin
@RequestMapping(value = "/order", method = POST)
@ApiOperation(value = "Place an order for a pet", response = Order.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
public ResponseEntity<String> placeOrder(
@ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
storeData.add(order);
return ok("");
}
~~~
## ApiResponses
ApiResponses:響應集配置,使用方式:
~~~
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
~~~
與Controller中的方法并列使用。 屬性配置:
| 屬性名稱 | 備注 |
| --- | --- |
| value | 多個ApiResponse配置 |
在SpringMvc中的配置如下:
~~~kotlin
@RequestMapping(value = "/order", method = POST)
@ApiOperation(value = "Place an order for a pet", response = Order.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
public ResponseEntity<String> placeOrder(
@ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
storeData.add(order);
return ok("");
}
~~~
## ResponseHeader
> 響應頭設置,使用方法
~~~kotlin
@ResponseHeader(name="head1",description="response head conf")
~~~
與Controller中的方法并列使用。 屬性配置:
| 屬性名稱 | 備注 |
| --- | --- |
| name | 響應頭名稱 |
| description | 頭描述 |
| response | 默認響應類 Void |
| responseContainer | 參考ApiOperation中配置 |
在SpringMvc中的配置如下:
~~~
@ApiModel(description = "群組")
~~~
## 其他
* @ApiImplicitParams:用在方法上包含一組參數說明;
* @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面
* paramType:參數放在哪個地方
* name:參數代表的含義
* value:參數名稱
* dataType: 參數類型,有String/int,無用
* required : 是否必要
* defaultValue:參數的默認值
* @ApiResponses:用于表示一組響應;
* @ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息;
* code: 響應碼(int型),可自定義
* message:狀態碼對應的響應信息
* @ApiModel:描述一個Model的信息(這種一般用在post創建的時候,使用@RequestBody這樣的場景,請求參數無法使用@ApiImplicitParam注解進行描述的時候;
* @ApiModelProperty:描述一個model的屬性。
paramType說的是你的參數類型,path說明參數是在url路徑上`/a/b/id`,query是說參數在get方法的查詢參數上`/a/b/c?id=1&name=tom`,這個就是swagger會影響參數接收的原因.
| | |
| --- | --- |
| name | 接收參數名 |
| value | 接收參數的意義描述 |
| required | 參數是否必填值為 true 或者 false|
| dataType | 參數的數據類型只作為標志說明,并沒有實際驗證|
| paramType | 查詢參數類型,其值 path 以地址的形式提交數據, query 直接跟參數完成自動映射 body 以流的形式提交僅?支持 POST, header 參數在 request headers 里邊提交, form 以 form 表單的形式提交 僅?支持 POST|
| defaultValue | 默認值 |
---
如果是上傳文件的話,需要把`@ApiImplicitParam`中的`dataType`屬性配置為`__File`否則在swagger中會顯示為文本框而不是上傳按鈕
# 配置
basePackage里面的包名要改下,寫哪里就掃哪里,可以寫控制器下的
~~~
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger 配置文件
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.two"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot利用Swagger構建API文檔")
.description("使用RestFul風格, 創建人:知了一笑")
.termsOfServiceUrl("https://github.com/cicadasmile")
.version("version 1.0")
.build();
}
}
~~~
## 所有的添加進去
配置文件,有了這個,不寫@EnableSwagger2這個也可以
~~~
swagger:
enabled: true
~~~
* @Configuration,啟動時加載此類
* @EnableSwagger2,表示此項?目啟?用 Swagger Api注解
~~~
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 創建一個Docket對象
* 調用select()方法,
* 生成ApiSelectorBuilder對象實例,該對象負責定義外漏的API入口
* 通過使用RequestHandlerSelectors和PathSelectors來提供Predicate,在此我們使用any()方法,將所有API都通過Swagger進行文檔管理
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
//可以看ApiInfo的詳細信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//標題
.title("Spring Boot中使用Swagger2構建RESTful APIs")
//簡介
.description("")
//服務條款
.termsOfServiceUrl("")
//作者個人信息
.contact(new Contact("chenguoyu","","chenguoyu_sir@163.com"))
//版本
.version("1.0")
.build();
}
}
~~~
# 啟動類添加注解
@EnableSwagger2可以在其他地方配置
~~~
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class,args) ;
}
}
~~~
打開`http://localhost:8080/swagger-ui.html`
# 增刪改查案例
## 添加用戶
~~~
@ApiOperation(value="添加用戶", notes="創建新用戶")
@ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User")
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public ResponseEntity addUser (@RequestBody User user){
~~~
## 用戶列表
~~~
@ApiOperation(value="用戶列表", notes="查詢用戶列表")
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public ResponseEntity getUserList (){
~~~
## 用戶查詢
~~~
@ApiOperation(value="用戶查詢", notes="根據ID查詢用戶")
@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer", paramType = "path")
@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
public ResponseEntity getUserById (@PathVariable(value = "id") Integer id){
~~~
## 更新用戶
~~~
@ApiOperation(value="更新用戶", notes="根據Id更新用戶信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long",paramType = "path"),
@ApiImplicitParam(name = "user", value = "用戶對象user", required = true, dataType = "User")
})
@RequestMapping(value = "/updateById/{id}", method = RequestMethod.PUT)
public ResponseEntity<JsonResult> updateById (@PathVariable("id") Integer id, @RequestBody User user){
~~~
## 刪除用戶
~~~
@ApiOperation(value="刪除用戶", notes="根據id刪除指定用戶")
@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long", paramType = "path")
@RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
public ResponseEntity<JsonResult> deleteById (@PathVariable(value = "id") Integer id){
~~~
# 404解決
~~~
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
~~~
**配置靜態訪問資源**
~~~
@Configuration
@EnableWebMvc
public class WebMvcConfigurerConfig implements WebMvcConfigurer {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
~~~
# 更改UI
由于個人感覺原生的swagger-ui不太好看,更改UI
[https://doc.xiaominfo.com/guide/](https://doc.xiaominfo.com/guide/)
~~~
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
~~~
配置靜態訪問資源
~~~
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 解決 swagger-ui.html 404報錯
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
// 解決 doc.html 404 報錯
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
}
}
~~~
# 離線文檔
**使用Swagger2Markup導出swagger2文檔**
~~~
<!--swagger靜態化輸出依賴-->
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>1.3.3</version>
</dependency>
~~~
~~~
<!--swagger靜態化插件 先執行測試類生成.adoc文件再運行maven命令 asciidoctor:process-asciidoc生成html-->
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.6</version>
<configuration>
<sourceDirectory>./docs/asciidoc/generated</sourceDirectory>
<outputDirectory>./docs/asciidoc/html</outputDirectory>
<headerFooter>true</headerFooter>
<doctype>book</doctype>
<backend>html</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<!--菜單欄在左邊-->
<toc>left</toc>
<!--多標題排列-->
<toclevels>3</toclevels>
<!--自動打數字序號-->
<sectnums>true</sectnums>
</attributes>
</configuration>
</plugin>
~~~
**完整的pom**
~~~
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<!--導出到markdown文件的依賴 -->
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>nl.jworks.markdown_to_asciidoc</groupId>
<artifactId>markdown_to_asciidoc</artifactId>
<version>1.1-sources</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--swagger靜態化插件 先執行測試類生成.adoc文件再運行maven命令 asciidoctor:process-asciidoc生成html-->
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.6</version>
<configuration>
<sourceDirectory>./docs/asciidoc/generated</sourceDirectory>
<outputDirectory>./docs/asciidoc/html</outputDirectory>
<headerFooter>true</headerFooter>
<doctype>book</doctype>
<backend>html</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<!--菜單欄在左邊-->
<toc>left</toc>
<!--多標題排列-->
<toclevels>3</toclevels>
<!--自動打數字序號-->
<sectnums>true</sectnums>
</attributes>
</configuration>
</plugin>
</plugins>
</build>
</project>
~~~
**寫一個測試類**
~~~
import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;
import java.nio.file.Paths;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationIntfTests {
/**
* 生成AsciiDocs格式文檔
* @throws Exception
*/
@Test
public void generateAsciiDocs() throws Exception {
// 輸出Ascii格式
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.ASCIIDOC)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build();
Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFolder(Paths.get("./docs/asciidoc/generated"));
}
/**
* 生成Markdown格式文檔
* @throws Exception
*/
@Test
public void generateMarkdownDocs() throws Exception {
// 輸出Markdown格式
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.MARKDOWN)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build();
Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFolder(Paths.get("./docs/markdown/generated"));
}
/**
* 生成Confluence格式文檔
* @throws Exception
*/
@Test
public void generateConfluenceDocs() throws Exception {
// 輸出Confluence使用的格式
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.CONFLUENCE_MARKUP)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build();
Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFolder(Paths.get("./docs/confluence/generated"));
}
/**
* 生成AsciiDocs格式文檔,并匯總成一個文件
* @throws Exception
*/
@Test
public void generateAsciiDocsToFile() throws Exception {
// 輸出Ascii到單文件
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.ASCIIDOC)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build();
Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFile(Paths.get("./docs/asciidoc/generated/all"));
}
/**
* 生成Markdown格式文檔,并匯總成一個文件
* @throws Exception
*/
@Test
public void generateMarkdownDocsToFile() throws Exception {
// 輸出Markdown到單文件
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.MARKDOWN)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build();
Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFile(Paths.get("./docs/markdown/generated/all"));
}
}
~~~
啟動項目,拿到json文件的地址,拿到之后關閉項目,不然之后允許測試的時候會報端口被占用
運行步驟中的五個Test,如果報docs/markdown文件夾不存在,請自己手動建文件夾;不出意外的話generated會生成.adoc文件
~~~
.
├── asciidoc
│?? └── generated
│?? ├── all.adoc
│?? ├── definitions.adoc
│?? ├── overview.adoc
│?? ├── paths.adoc
│?? └── security.adoc
├── confluence
│?? └── generated
│?? ├── definitions.txt
│?? ├── overview.txt
│?? ├── paths.txt
│?? └── security.txt
└── markdown
└── generated
├── all.md
├── definitions.md
├── overview.md
├── paths.md
└── security.md
~~~
運行maven命令asciidoctor:process-asciidoc生成html
~~~
mvn asciidoctor:process-asciidoc
~~~
大功告成,訪問all.html即可
~~~
├── asciidoc
│?? ├── generated
│?? │?? ├── all.adoc
│?? │?? ├── definitions.adoc
│?? │?? ├── overview.adoc
│?? │?? ├── paths.adoc
│?? │?? └── security.adoc
│?? └── html
│?? ├── all.html
│?? ├── definitions.html
│?? ├── overview.html
│?? ├── paths.html
│?? └── security.html
├── confluence
│?? └── generated
│?? ├── definitions.txt
│?? ├── overview.txt
│?? ├── paths.txt
│?? └── security.txt
└── markdown
└── generated
├── all.md
├── definitions.md
├── overview.md
├── paths.md
└── security.md
~~~
# 注解總結
~~~
@Api:用在請求的類上,表示對類的說明
tags="說明該類的作用,可以在UI界面上看到的注解"
value="該參數沒什么意義,在UI界面上也看到,所以不需要配置"
@ApiOperation:用在請求的方法上,說明方法的用途、作用
value="說明方法的用途、作用"
notes="方法的備注說明"
@ApiImplicitParams:用在請求的方法上,表示一組參數說明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面
name:參數名
value:參數的漢字說明、解釋
required:參數是否必須傳
paramType:參數放在哪個地方
· header --> 請求參數的獲取:@RequestHeader
· query --> 請求參數的獲取:@RequestParam
· path(用于restful接口)--> 請求參數的獲取:@PathVariable
· body(不常用)
· form(不常用)
dataType:參數類型,默認String,其它值dataType="Integer"
defaultValue:參數的默認值
@ApiResponses:用在請求的方法上,表示一組響應
@ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息
code:數字,例如400
message:信息,例如"請求參數沒填好"
response:拋出異常的類
@ApiModel:用于響應類上,表示一個返回響應數據的信息
(這種一般用在post創建的時候,使用@RequestBody這樣的場景,
請求參數無法使用@ApiImplicitParam注解進行描述的時候)
@ApiModelProperty:用在屬性上,描述響應類的屬性
~~~
1、@Api:用在請求的類上,說明該類的作用
? ? ?tags="說明該類的作用"
? ? ?value="該參數沒什么意義,所以不需要配置"
示例:
~~~
@Api(tags="APP用戶注冊Controller")
~~~
2、@ApiOperation:用在請求的方法上,說明方法的作用
@ApiOperation:"用在請求的方法上,說明方法的作用"
? ? value="說明方法的作用"
? ? notes="方法的備注說明"
示例:
~~~
@ApiOperation(value="用戶注冊",notes="手機號、密碼都是必輸項,年齡隨邊填,但必須是數字")
~~~
3、@ApiImplicitParams:用在請求的方法上,包含一組參數說明
? ? ?@ApiImplicitParams:用在請求的方法上,包含一組參數說明
? ? ?@ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一個請求參數的配置信息 ? ? ??
? ? ? ? name:參數名
? ? ? ? value:參數的漢字說明、解釋
? ? ? ? required:參數是否必須傳
? ? ? ? paramType:參數放在哪個地方
? ? ? ? ? ? · header --> 請求參數的獲取:@RequestHeader
? ? ? ? ? ? · query --> 請求參數的獲取:@RequestParam
? ? ? ? ? ? · path(用于restful接口)--> 請求參數的獲取:@PathVariable
? ? ? ? ? ? · body(不常用)
? ? ? ? ? ? · form(不常用) ? ?
? ? ? ? dataType:參數類型,默認String,其它值dataType="Integer" ? ? ??
? ? ? ? defaultValue:參數的默認值
示列:
~~~java
@ApiImplicitParams({ @ApiImplicitParam(name="mobile",value="手機號",required=true,paramType="form"),? ? @ApiImplicitParam(name="password",value="密碼",required=true,paramType="form"),? ? @ApiImplicitParam(name="age",value="年齡",required=true,paramType="form",dataType="Integer")})
~~~
4、@ApiResponses:用于請求的方法上,表示一組響應
? ? ?@ApiResponses:用于請求的方法上,表示一組響應
? ? ?@ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息
? ? ? ? code:數字,例如400
? ? ? ? message:信息,例如"請求參數沒填好"
? ? ? ? response:拋出異常的類
示例:
~~~java
@ApiOperation(value = "select1請求",notes = "多個參數,多種的查詢參數類型")@ApiResponses({? ? @ApiResponse(code=400,message="請求參數沒填好"),? ? @ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對")})
~~~
5、@ApiModel:用于響應類上,表示一個返回響應數據的信息
? ? ?@ApiModel:用于響應類上,表示一個返回響應數據的信息
? ? ? ? ? ? (這種一般用在post創建的時候,使用@RequestBody這樣的場景,
? ? ? ? ? ? 請求參數無法使用@ApiImplicitParam注解進行描述的時候)
? ? ?@ApiModelProperty:用在屬性上,描述響應類的屬性
示例:
~~~java
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(description= "返回響應數據")
public class RestMessage implements Serializable{
? ? @ApiModelProperty(value = "是否成功")
? ? private boolean success=true;
? ? @ApiModelProperty(value = "返回對象")
? ? private Object data;
? ? @ApiModelProperty(value = "錯誤編號")
? ? private Integer errCode;
? ? @ApiModelProperty(value = "錯誤信息")
? ? private String message;
? ? /* getter/setter */
}
~~~
- 基礎
- 編譯和安裝
- classpath到底是什么?
- 編譯運行
- 安裝
- sdkman多版本
- jabba多版本
- java字節碼查看
- 數據類型
- 簡介
- 整形
- char和int
- 變量和常量
- 大數值運算
- 基本類型包裝類
- Math類
- 內存劃分
- 位運算符
- 方法相關
- 方法重載
- 可變參數
- 方法引用
- 面向對象
- 定義
- 繼承和覆蓋
- 接口和抽象類
- 接口定義增強
- 內建函數式接口
- 多態
- 泛型
- final和static
- 內部類
- 包
- 修飾符
- 異常
- 枚舉類
- 代碼塊
- 對象克隆
- BeanUtils
- java基礎類
- scanner類
- Random類
- System類
- Runtime類
- Comparable接口
- Comparator接口
- MessageFormat類
- NumberFormat
- 數組相關
- 數組
- Arrays
- string相關
- String
- StringBuffer
- StringBuilder
- 正則
- 日期類
- Locale類
- Date
- DateFormat
- SimpleDateFormat
- Calendar
- 新時間日期API
- 簡介
- LocalDate,LocalTime,LocalDateTime
- Instant時間點
- 帶時區的日期,時間處理
- 時間間隔
- 日期時間校正器
- TimeUnit
- 用yyyy
- 集合
- 集合和迭代器
- ArrayList集合
- List
- Set
- 判斷集合唯一
- Map和Entry
- stack類
- Collections集合工具類
- Stream數據流
- foreach不能修改內部元素
- of方法
- IO
- File類
- 字節流stream
- 字符流Reader
- IO流分類
- 轉換流
- 緩沖流
- 流的操作規律
- properties
- 序列化流與反序列化流
- 打印流
- System類對IO支持
- commons-IO
- IO流總結
- NIO
- 異步與非阻塞
- IO通信
- Unix的IO模型
- epoll對于文件描述符操作模式
- 用戶空間和內核空間
- NIO與普通IO的主要區別
- Paths,Path,Files
- Buffer
- Channel
- Selector
- Pipe
- Charset
- NIO代碼
- 多線程
- 創建線程
- 線程常用方法
- 線程池相關
- 線程池概念
- ThreadPoolExecutor
- Runnable和Callable
- 常用的幾種線程池
- 線程安全
- 線程同步的幾種方法
- synchronized
- 死鎖
- lock接口
- ThreadLoad
- ReentrantLock
- 讀寫鎖
- 鎖的相關概念
- volatile
- 釋放鎖和不釋放鎖的操作
- 等待喚醒機制
- 線程狀態
- 守護線程和普通線程
- Lamda表達式
- 反射相關
- 類加載器
- 反射
- 注解
- junit注解
- 動態代理
- 網絡編程相關
- 簡介
- UDP
- TCP
- 多線程socket上傳圖片
- NIO
- JDBC相關
- JDBC
- 預處理
- 批處理
- 事務
- properties配置文件
- DBUtils
- DBCP連接池
- C3P0連接池
- 獲得MySQL自動生成的主鍵
- Optional類
- Jigsaw模塊化
- 日志相關
- JDK日志
- log4j
- logback
- xml
- tomcat
- maven
- 簡介
- 倉庫
- 目錄結構
- 常用命令
- 生命周期
- idea配置
- jar包沖突
- 依賴范圍
- 私服
- 插件
- git-commit-id-plugin
- maven-assembly-plugin
- maven-resources-plugin
- maven-compiler-plugin
- versions-maven-plugin
- maven-source-plugin
- tomcat-maven-plugin
- 多環境
- 自定義插件
- stream
- swing
- json
- jackson
- optional
- junit
- gradle
- servlet
- 配置
- ServletContext
- 生命周期
- HttpServlet
- request
- response
- 亂碼
- session和cookie
- cookie
- session
- jsp
- 簡介
- 注釋
- 方法,成員變量
- 指令
- 動作標簽
- 隱式對象
- EL
- JSTL
- javaBean
- listener監聽器
- Filter過濾器
- 圖片驗證碼
- HttpUrlConnection
- 國際化
- 文件上傳
- 文件下載
- spring
- 簡介
- Bean
- 獲取和實例化
- 屬性注入
- 自動裝配
- 繼承和依賴
- 作用域
- 使用外部屬性文件
- spel
- 前后置處理器
- 生命周期
- 掃描規則
- 整合多個配置文件
- 注解
- 簡介
- 注解分層
- 類注入
- 分層和作用域
- 初始化方法和銷毀方法
- 屬性
- 泛型注入
- Configuration配置文件
- aop
- aop的實現
- 動態代理實現
- cglib代理實現
- aop名詞
- 簡介
- aop-xml
- aop-注解
- 代理方式選擇
- jdbc
- 簡介
- JDBCTemplate
- 事務
- 整合
- junit整合
- hibernate
- 簡介
- hibernate.properties
- 實體對象三種狀態
- 檢索方式
- 簡介
- 導航對象圖檢索
- OID檢索
- HQL
- Criteria(QBC)
- Query
- 緩存
- 事務管理
- 關系映射
- 注解
- 優化
- MyBatis
- 簡介
- 入門程序
- Mapper動態代理開發
- 原始Dao開發
- Mapper接口開發
- SqlMapConfig.xml
- map映射文件
- 輸出返回map
- 輸入參數
- pojo包裝類
- 多個輸入參數
- resultMap
- 動態sql
- 關聯
- 一對一
- 一對多
- 多對多
- 整合spring
- CURD
- 占位符和sql拼接以及參數處理
- 緩存
- 延遲加載
- 注解開發
- springMVC
- 簡介
- RequestMapping
- 參數綁定
- 常用注解
- 響應
- 文件上傳
- 異常處理
- 攔截器
- springBoot
- 配置
- 熱更新
- java配置
- springboot配置
- yaml語法
- 運行
- Actuator 監控
- 多環境配置切換
- 日志
- 日志簡介
- logback和access
- 日志文件配置屬性
- 開機自啟
- aop
- 整合
- 整合Redis
- 整合Spring Data JPA
- 基本查詢
- 復雜查詢
- 多數據源的支持
- Repository分析
- JpaSpeci?cationExecutor
- 整合Junit
- 整合mybatis
- 常用注解
- 基本操作
- 通用mapper
- 動態sql
- 關聯映射
- 使用xml
- spring容器
- 整合druid
- 整合郵件
- 整合fastjson
- 整合swagger
- 整合JDBC
- 整合spingboot-cache
- 請求
- restful
- 攔截器
- 常用注解
- 參數校驗
- 自定義filter
- websocket
- 響應
- 異常錯誤處理
- 文件下載
- 常用注解
- 頁面
- Thymeleaf組件
- 基本對象
- 內嵌對象
- 上傳文件
- 單元測試
- 模擬請求測試
- 集成測試
- 源碼解析
- 自動配置原理
- 啟動流程分析
- 源碼相關鏈接
- Servlet,Filter,Listener
- springcloud
- 配置
- 父pom
- 創建子工程
- Eureka
- Hystrix
- Ribbon
- Feign
- Zuul
- kotlin
- 基本數據類型
- 函數
- 區間
- 區塊鏈
- 簡介
- linux
- ulimit修改
- 防止syn攻擊
- centos7部署bbr
- debain9開啟bbr
- mysql
- 隔離性
- sql執行加載順序
- 7種join
- explain
- 索引失效和優化
- 表連接優化
- orderby的filesort問題
- 慢查詢
- show profile
- 全局查詢日志
- 死鎖解決
- sql
- 主從
- IDEA
- mac快捷鍵
- 美化界面
- 斷點調試
- 重構
- springboot-devtools熱部署
- IDEA進行JAR打包
- 導入jar包
- ProjectStructure
- toString添加json模板
- 配置maven
- Lombok插件
- rest client
- 文檔顯示
- sftp文件同步
- 書簽
- 代碼查看和搜索
- postfix
- live template
- git
- 文件頭注釋
- JRebel
- 離線模式
- xRebel
- github
- 連接mysql
- 選項沒有Java class的解決方法
- 擴展
- 項目配置和web部署
- 前端開發
- json和Inject language
- idea內存和cpu變高
- 相關設置
- 設計模式
- 單例模式
- 簡介
- 責任鏈
- JUC
- 原子類
- 原子類簡介
- 基本類型原子類
- 數組類型原子類
- 引用類型原子類
- JVM
- JVM規范內存解析
- 對象的創建和結構
- 垃圾回收
- 內存分配策略
- 備注
- 虛擬機工具
- 內存模型
- 同步八種操作
- 內存區域大小參數設置
- happens-before
- web service
- tomcat
- HTTPS
- nginx
- 變量
- 運算符
- 模塊
- Rewrite規則
- Netty
- netty為什么沒用AIO
- 基本組件
- 源碼解讀
- 簡單的socket例子
- 準備netty
- netty服務端啟動
- 案例一:發送字符串
- 案例二:發送對象
- websocket
- ActiveMQ
- JMS
- 安裝
- 生產者-消費者代碼
- 整合springboot
- kafka
- 簡介
- 安裝
- 圖形化界面
- 生產過程分析
- 保存消息分析
- 消費過程分析
- 命令行
- 生產者
- 消費者
- 攔截器interceptor
- partition
- kafka為什么快
- kafka streams
- kafka與flume整合
- RabbitMQ
- AMQP
- 整體架構
- RabbitMQ安裝
- rpm方式安裝
- 命令行和管控頁面
- 消息生產與消費
- 整合springboot
- 依賴和配置
- 簡單測試
- 多方測試
- 對象支持
- Topic Exchange模式
- Fanout Exchange訂閱
- 消息確認
- java client
- RabbitAdmin和RabbitTemplate
- 兩者簡介
- RabbitmqAdmin
- RabbitTemplate
- SimpleMessageListenerContainer
- MessageListenerAdapter
- MessageConverter
- 詳解
- Jackson2JsonMessageConverter
- ContentTypeDelegatingMessageConverter
- lucene
- 簡介
- 入門程序
- luke查看索引
- 分析器
- 索引庫維護
- elasticsearch
- 配置
- 插件
- head插件
- ik分詞插件
- 常用術語
- Mapping映射
- 數據類型
- 屬性方法
- Dynamic Mapping
- Index Template 索引模板
- 管理映射
- 建立映射
- 索引操作
- 單模式下CURD
- mget多個文檔
- 批量操作
- 版本控制
- 基本查詢
- Filter過濾
- 組合查詢
- 分析器
- redis
- String
- list
- hash
- set
- sortedset
- 發布訂閱
- 事務
- 連接池
- 管道
- 分布式可重入鎖
- 配置文件翻譯
- 持久化
- RDB
- AOF
- 總結
- Lettuce
- zookeeper
- zookeeper簡介
- 集群部署
- Observer模式
- 核心工作機制
- zk命令行操作
- zk客戶端API
- 感知服務動態上下線
- 分布式共享鎖
- 原理
- zab協議
- 兩階段提交協議
- 三階段提交協議
- Paxos協議
- ZAB協議
- hadoop
- 簡介
- hadoop安裝
- 集群安裝
- 單機安裝
- linux編譯hadoop
- 添加新節點
- 退役舊節點
- 集群間數據拷貝
- 歸檔
- 快照管理
- 回收站
- 檢查hdfs健康狀態
- 安全模式
- hdfs簡介
- hdfs命令行操作
- 常見問題匯總
- hdfs客戶端操作
- mapreduce工作機制
- 案例-單詞統計
- 局部聚合Combiner
- combiner流程
- combiner案例
- 自定義排序
- 自定義Bean對象
- 排序的分類
- 案例-按總量排序需求
- 一次性完成統計和排序
- 分區
- 分區簡介
- 案例-結果分區
- 多表合并
- reducer端合并
- map端合并(分布式緩存)
- 分組
- groupingComparator
- 案例-求topN
- 全局計數器
- 合并小文件
- 小文件的弊端
- CombineTextInputFormat機制
- 自定義InputFormat
- 自定義outputFormat
- 多job串聯
- 倒排索引
- 共同好友
- 串聯
- 數據壓縮
- InputFormat接口實現類
- yarn簡介
- 推測執行算法
- 本地提交到yarn
- 框架運算全流程
- 數據傾斜問題
- mapreduce的優化方案
- HA機制
- 優化
- Hive
- 安裝
- shell參數
- 數據類型
- 集合類型
- 數據庫
- DDL操作
- 創建表
- 修改表
- 分區表
- 分桶表
- DML操作
- load
- insert
- select
- export,import
- Truncate
- 注意
- 嚴格模式
- 函數
- 內置運算符
- 內置函數
- 自定義函數
- Transfrom實現
- having和where不同
- 壓縮
- 存儲
- 存儲和壓縮結合使用
- explain詳解
- 調優
- Fetch抓取
- 本地模式
- 表的優化
- GroupBy
- count(Distinct)去重統計
- 行列過濾
- 動態分區調整
- 數據傾斜
- 并行執行
- JVM重用
- 推測執行
- reduce內存和個數
- sql查詢結果作為變量(shell)
- youtube
- flume
- 簡介
- 安裝
- 常用組件
- 攔截器
- 案例
- 監聽端口到控制臺
- 采集目錄到HDFS
- 采集文件到HDFS
- 多個agent串聯
- 日志采集和匯總
- 單flume多channel,sink
- 自定義攔截器
- 高可用配置
- 使用注意
- 監控Ganglia
- sqoop
- 安裝
- 常用命令
- 數據導入
- 準備數據
- 導入數據到HDFS
- 導入關系表到HIVE
- 導入表數據子集
- 增量導入
- 數據導出
- 打包腳本
- 作業
- 原理
- azkaban
- 簡介
- 安裝
- 案例
- 簡介
- command類型單一job
- command類型多job工作流flow
- HDFS操作任務
- mapreduce任務
- hive腳本任務
- oozie
- 安裝
- hbase
- 簡介
- 系統架構
- 物理存儲
- 尋址機制
- 讀寫過程
- 安裝
- 命令行
- 基本CURD
- java api
- CURD
- CAS
- 過濾器查詢
- 建表高級屬性
- 與mapreduce結合
- 與sqoop結合
- 協處理器
- 參數配置優化
- 數據備份和恢復
- 節點管理
- 案例-點擊流
- 簡介
- HUE
- 安裝
- storm
- 簡介
- 安裝
- 集群啟動及任務過程分析
- 單詞統計
- 單詞統計(接入kafka)
- 并行度和分組
- 啟動流程分析
- ACK容錯機制
- ACK簡介
- BaseRichBolt簡單使用
- BaseBasicBolt簡單使用
- Ack工作機制
- 本地目錄樹
- zookeeper目錄樹
- 通信機制
- 案例
- 日志告警
- 工具
- YAPI
- chrome無法手動拖動安裝插件
- 時間和空間復雜度
- jenkins
- 定位cpu 100%
- 常用腳本工具
- OOM問題定位
- scala
- 編譯
- 基本語法
- 函數
- 數組常用方法
- 集合
- 并行集合
- 類
- 模式匹配
- 異常
- tuple元祖
- actor并發編程
- 柯里化
- 隱式轉換
- 泛型
- 迭代器
- 流stream
- 視圖view
- 控制抽象
- 注解
- spark
- 企業架構
- 安裝
- api開發
- mycat
- Groovy
- 基礎