swagger,中文「拽」的意思。它是一個功能強大的api框架,它的集成很是簡單,不只提供了在線文檔的查閱,並且還提供了在線文檔的測試。另外swagger很容易構建restful風格的api,簡單優雅帥氣,正如它的名字。java
1、引入依賴web
<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、寫配置類spring
@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()指定掃描的包會生成文檔。json
3、寫生產文檔的註解api
swagger經過註解代表該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。springboot
如今經過一個栗子來講明:restful
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!"; } }
Spring Cloud大型企業分佈式微服務雲架構源碼請加企鵝求求:一七九一七四三三八零架構