SpringBoot 整合swageer2 生成RestFul API文檔
阿新 • • 發佈:2019-03-25
use 用戶 red arraylist enables ces prop clas rip
1.首先建立SpringBoot項目,導入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>
2.建立一個swagge2註冊類
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; /*** * @ClassName: swagger2註冊類 * @Description: s * @Auther: ywang * @Date: 15:03 2019/3/25 * @version : V1.0 */ @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //為當前包路徑 .apis(RequestHandlerSelectors.basePackage("com.example.web")) .paths(PathSelectors.any()) .build(); } //構建 api文檔的詳細信息函數 private ApiInfo apiInfo() { return new ApiInfoBuilder() //頁面標題 .title("Spring Boot 測試使用 Swagger2 構建RESTful API") //創建人 .contact(new Contact("yw", "http://www.baidu.com", "[email protected]")) //版本號 .version("1.0") //描述 .description("API 描述") .build(); } }
3.建立實體類
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /*** * @ClassName: User * @Description: 用戶實體類 * @Auther: ywang * @Date: 14:03 2019/3/25 * @version : V1.0 */ @ApiModel(description="用戶對象user") public class User { @ApiModelProperty(value="id",example="1") private Long id; @ApiModelProperty(value="姓名",example="張三") private String name; @ApiModelProperty(value="年齡",example="23") private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
4.建立一個controller
import java.util.*; import com.example.domain.User; import io.swagger.annotations.*; import org.springframework.web.bind.annotation.*; /*** * @ClassName: UserController * @Description: 程序入口類 * @Auther: ywang * @Date: 14:04 2019/3/25 * @version : V1.0 */ @Api(value="用戶controller",tags={"用戶操作接口"}) @RestController @RequestMapping(value="/users") // 通過這裏配置使下面的映射都在/users下,可去除 public class UserController { static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); /** * @Param [] * @Description: TODO * @auther: ywang * @date: 14:07 2019/3/25 * @return: java.util.List<com.example.domain.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", paramType = "path") @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", paramType = "path"), @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", paramType = "path") @RequestMapping(value="/{id}", method=RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { users.remove(id); return "success"; } }
5.測試
啟動項目,http://localhost:8080/swagger-ui.html
SpringBoot 整合swageer2 生成RestFul API文檔