Swagger 2是一個開源項目,用於描述和記錄RESTful API。Swagger 2與語言無關,可擴展到HTTP以外的新技術和協議。當前版本定義了一組HTML,JavaScript和CSS資產,以從符合Swagger的API動態生成文檔。這些文件由Swagger UI項目捆綁在一塊兒,以便在瀏覽器上顯示API。除了渲染文檔外,Swagger UI容許其餘API開發人員或消費者與API的資源進行交互,而無需執行任何實現邏輯。html
首先在 pom中 加入 swagger2依賴--以下:spring
<!--pom.xml中加入Swagger2的依賴--> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>1.4.2.RELEASE</version> <scope>test</scope> </dependency>
在這個控制器中,Spring 4.0引入的 @ RestController註釋將ProductController標記 爲REST API控制器。在引擎蓋下, @ RestController做爲一個方便的註釋,用@ 控制器和 @ ResponseBody來註釋類 。api
@RestController @RequestMapping(value="/users") // 經過這裏配置使下面的映射都在/users下,可去除 public class UserController { static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); @ApiOperation(value="獲取用戶列表", notes="") @RequestMapping(value={""}, method=RequestMethod.GET) public List<User> getUserList() { List<User> r = new ArrayList<User>(users.values()); return r; } @ApiOperation(value="建立用戶", notes="根據User對象建立用戶") @ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User") @RequestMapping(value="", method=RequestMethod.POST) public String postUser(@RequestBody User user) { users.put(user.getId(), user); return "success"; } @ApiOperation(value="獲取用戶詳細信息", notes="根據url的id來獲取用戶詳細信息") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.GET) public User getUser(@PathVariable Long id) { return users.get(id); } @ApiOperation(value="更新用戶詳細信息", notes="根據url的id來指定更新對象,並根據傳過來的user信息來更新用戶詳細信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long"), @ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User") }) @RequestMapping(value="/{id}", method=RequestMethod.PUT) public String putUser(@PathVariable Long id, @RequestBody User user) { User u = users.get(id); u.setName(user.getName()); u.setAge(user.getAge()); users.put(id, u); return "success"; } @ApiOperation(value="刪除用戶", notes="根據url的id來指定刪除對象") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { users.remove(id); return "success"; } }
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.huangyf")) .paths(PathSelectors.any()) .build(); } }
其中 basePackage 是指定 包名瀏覽器
最後打開 http://localhost:8080/swagger-ui.html 就能看到效果了springboot
這次 參考 https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/#comment-2134
以及 http://blog.didispace.com/springbootswagger2/ 鳴謝二位restful