swagger,中文「拽」的意思。 它是一個功能強大的api框架,它的集成很是簡單,不只提供了在線文檔的查閱,並且還提供了在線文檔的測試。另外swagger很容易構建restful風格的api。java
1、Pom.xml 中引入依賴 lweb
<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、寫生產文檔的註解 swagger經過註解代表該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。api
@Api:修飾整個類,描述Controller的做用 @ApiOperation:描述一個類的一個方法,或者說一個接口 @ApiParam:單個參數描述 @ApiModel:用對象來接收參數 @ApiProperty:用對象接收參數時,描述對象的一個字段 @ApiResponse:HTTP響應其中1個描述 @ApiResponses:HTTP響應總體描述 @ApiIgnore:使用該註解忽略這個API @ApiError :發生錯誤返回的信息 @ApiParamImplicitL:一個請求參數 @ApiParamsImplicit 多個請求參數
舉個栗子來講明:springboot
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」 。 restful