swagger,中文「拽」的意思。它是一個功能強大的api框架,它的集成很是簡單,不只提供了在線文檔的查閱,並且還提供了在線文檔的測試。java
另外swagger很容易構建restful風格的api,簡單優雅帥氣,正如它的名字。web
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
import org.springframework.beans.factory.annotation.Value; 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; @Configuration @EnableSwagger2 public class SwaggerConfig { Boolean swaggerEnabled = true; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // 是否開啓 .enable(swaggerEnabled)//true .select() .apis(RequestHandlerSelectors.basePackage("com.user.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() //頁面標題 .title(" 測試使用 ") //建立人 .contact(new Contact("clc", "http://www.baidu.com", "")) //版本號 .version("1.0") //描述 .description("API 描述") .build(); } }
經過@Configuration註解,代表它是一個配置類,spring
@EnableSwagger2開啓swagger2。json
apiINfo()配置一些基本的信息。apis()指定掃描的包會生成文檔。api
swagger經過註解代表該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。restful
如今經過一個栗子來講明:app
package clc.user.controller; import clc.user.bean.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * ClassName: TestSwaggerController<br/> * Description: <br/> * date: 2018/11/30 10:06 AM<br/> * * @author chengluchao * @since JDK 1.8 */ @Api(tags = "用戶API") @RestController public class TestSwaggerController { Map<Object, User> users = Collections.synchronizedMap(new HashMap<Object, User>()); @ApiOperation(value = "獲取用戶列表", notes = "獲取用戶列表") @RequestMapping(value = {"/getUser1"}, method = RequestMethod.GET) public List<User> getUser() { List<User> user = new ArrayList<>(users.values()); return user; } @ApiOperation(value = "建立用戶", notes = "建立用戶") @ApiImplicitParam(name = "user", value = "用戶詳細實體", required = true, dataType = "User") @RequestMapping(value = "/createUser", method = RequestMethod.POST) public String postUser(@RequestBody User user) { users.put(user.getAge(), user); return "success"; } @ApiOperation(value = "獲用戶細信息", notes = "根據url的id來獲取詳細信息") @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long", paramType = "path") @RequestMapping(value = "/byid", method = RequestMethod.GET) public User getUser(@PathVariable Long id) { return users.get(id); } @ApiOperation(value = "更新信息", notes = "根據url的id來指定更新用戶信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long", paramType = "path"), @ApiImplicitParam(name = "user", value = "用戶實體user", required = true, dataType = "User") }) @RequestMapping(value = "/putid", method = RequestMethod.PUT) public String putUser(@PathVariable Long id, @RequestBody User user) { User user1 = users.get(id); user1.setName(user.getName()); users.put(id, user1); return "success"; } @ApiOperation(value = "刪除用戶", notes = "根據url的id來指定刪除用戶") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long", paramType = "path") @RequestMapping(value = "/delid", method = RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { users.remove(id); return "success"; } @ApiIgnore//使用該註解忽略這個API @RequestMapping(value = "/hi", method = RequestMethod.GET) public String jsonTest() { return " hi you!"; } }