swagger,中文「拽」的意思。它是一個功能強大的api框架,它的集成很是簡單,不只提供了在線文檔的查閱,並且還提供了在線文檔的測試。另外swagger很容易構建restful風格的api,簡單優雅帥氣,正如它的名字。html
1、引入依賴java
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>複製代碼
2、寫配置類web
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.forezp.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger構建api文檔")
.description("簡單優雅的restfun風格,http://blog.csdn.net/forezp")
.termsOfServiceUrl("http://blog.csdn.net/forezp")
.version("1.0")
.build();
}
}複製代碼
經過@Configuration註解,代表它是一個配置類,@EnableSwagger2開啓swagger2。apiINfo()配置一些基本的信息。apis()指定掃描的包會生成文檔。spring
3、寫生產文檔的註解json
swagger經過註解代表該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。api
如今經過一個栗子來講明:springboot
package com.forezp.controller;
import com.forezp.entity.Book;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.*;
/**
* 用戶建立某本圖書 POST /books/
* 用戶修改對某本圖書 PUT /books/:id/
* 用戶刪除對某本圖書 DELETE /books/:id/
* 用戶獲取全部的圖書 GET /books
* 用戶獲取某一圖書 GET /Books/:id
* Created by fangzhipeng on 2017/4/17.
* 官方文檔:http://swagger.io/docs/specification/api-host-and-base-path/
*/
@RestController
@RequestMapping(value = "/books")
public class BookContrller {
Map<Long, Book> books = Collections.synchronizedMap(new HashMap<Long, Book>());
@ApiOperation(value="獲取圖書列表", notes="獲取圖書列表")
@RequestMapping(value={""}, method= RequestMethod.GET)
public List<Book> getBook() {
List<Book> book = new ArrayList<>(books.values());
return book;
}
@ApiOperation(value="建立圖書", notes="建立圖書")
@ApiImplicitParam(name = "book", value = "圖書詳細實體", required = true, dataType = "Book")
@RequestMapping(value="", method=RequestMethod.POST)
public String postBook(@RequestBody Book book) {
books.put(book.getId(), book);
return "success";
}
@ApiOperation(value="獲圖書細信息", notes="根據url的id來獲取詳細信息")
@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long",paramType = "path")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public Book getBook(@PathVariable Long id) {
return books.get(id);
}
@ApiOperation(value="更新信息", notes="根據url的id來指定更新圖書信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "圖書ID", required = true, dataType = "Long",paramType = "path"),
@ApiImplicitParam(name = "book", value = "圖書實體book", required = true, dataType = "Book")
})
@RequestMapping(value="/{id}", method= RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody Book book) {
Book book1 = books.get(id);
book1.setName(book.getName());
book1.setPrice(book.getPrice());
books.put(id, book1);
return "success";
}
@ApiOperation(value="刪除圖書", notes="根據url的id來指定刪除圖書")
@ApiImplicitParam(name = "id", value = "圖書ID", required = true, dataType = "Long",paramType = "path")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
books.remove(id);
return "success";
}
@ApiIgnore//使用該註解忽略這個API
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String jsonTest() {
return " hi you!";
}
}複製代碼
經過相關注解,就可讓swagger2生成相應的文檔。若是你不須要某接口生成文檔,只須要在加@ApiIgnore註解便可。須要說明的是,若是請求參數在url上,@ApiImplicitParam 上加paramType = 「path」 。bash
資料和源碼來源地址restful
Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六
app