官方介紹:Swagger是一個規範且完整的框架,提供描述、生產、消費和可視化RESTful Web Service。
專業角度:Swagger是由龐大工具集合支撐的形式化規範。這個集合涵蓋了從終端用戶接口、底層代碼庫到商業API管理的方方面面。html
<!-- 設置使用註解的類所在的jar包,只加載controller類 --> 前端
packagecom.jay.plat.config.util; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.ComponentScan; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.servlet.config.annotation.EnableWebMvc; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; importspringfox.documentation.builders.ApiInfoBuilder; importspringfox.documentation.builders.PathSelectors; importspringfox.documentation.builders.RequestHandlerSelectors; importspringfox.documentation.service.ApiInfo; importspringfox.documentation.spi.DocumentationType; importspringfox.documentation.spring.web.plugins.Docket; importspringfox.documentation.swagger2.annotations.EnableSwagger2; /* *RestfulAPI訪問路徑: *http://IP:port/{context-path}/swagger-ui.html *eg:http://localhost:8080/jd-config-web/swagger-ui.html */ @EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = {"com.<spanstyle="font - family:Arial, Helvetica, sans-serif;">jay.</span>plat.config.controller"}) @Configuration publicclassRestApiConfigextendsWebMvcConfigurationSupport{ @Bean publicDocketcreateRestApi(){ returnnewDocket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.jay.plat.config.controller")) .paths(PathSelectors.any()) .build(); } privateApiInfoapiInfo(){ returnnewApiInfoBuilder() .title("Spring中使用Swagger2構建RESTfulAPIs") .termsOfServiceUrl("http://blog.csdn.net/he90227") .contact("逍遙飛鶴") .version("1.1") .build(); } }
配置說明:java
packagecom.jay.spring.boot.demo10.swagger2.controller; importjava.util.ArrayList; importjava.util.Collections; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importorg.springframework.web.bind.annotation.PathVariable; importorg.springframework.web.bind.annotation.RequestBody; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RestController; importcom.jay.spring.boot.demo10.swagger2.bean.User; importio.swagger.annotations.ApiImplicitParam; importio.swagger.annotations.ApiImplicitParams; importio.swagger.annotations.ApiOperation; @RestController @RequestMapping(value = "/users")//經過這裏配置使下面的映射都在/users下,可去除 publicclassUserController{ staticMap<Long, User>users=Collections.synchronizedMap(newHashMap<Long, User>()); @ApiOperation(value = "獲取用戶列表", notes = "") @RequestMapping(value = {""}, method = RequestMethod.GET) publicList<User>getUserList(){ List<User>r=newArrayList<User>(users.values()); returnr; } @ApiOperation(value = "建立用戶", notes = "根據User對象建立用戶") @ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User") @RequestMapping(value = "", method = RequestMethod.POST) publicStringpostUser(@RequestBodyUseruser){ 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) publicUsergetUser(@PathVariableLongid){ returnusers.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) publicStringputUser(@PathVariableLongid,@RequestBodyUseruser){ Useru=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) publicStringdeleteUser(@PathVariableLongid){ users.remove(id); return"success"; } }
4.效果展現程序員
參考http://blog.csdn.net/jia20003/article/details/50700736web