<!-- swagger RESTful API 文檔 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
package com.sam.demo.domain; /** * @author sam * @since 2017/7/17 */ public class User { private Long id; private String name; private int age; //getter & setter }
package com.sam.demo; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author sam * @since 2017/7/17 */ @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { ApiInfo apiInfo = new ApiInfoBuilder() .title("Sam 項目接口文檔") .description("Magical Sam 項目的接口文檔,符合RESTful API。") .version("1.0") .build(); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() .apis(RequestHandlerSelectors.basePackage("com.sam.demo.controller")) //以掃描包的方式 .paths(PathSelectors.any()) .build(); } }
package com.sam.demo.controller; import com.sam.demo.domain.User; import io.swagger.annotations.*; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; /** * @author sam * @since 2017/7/14 */ @Api(value = "用戶模塊") @RestController @RequestMapping("/user") public class UserController { /** * 獲取單個用戶 * * @param id * @return */ @ApiOperation(value = "獲取單個用戶", notes = "傳入id獲取單個用戶") // @ApiImplicitParam(name = "id", value = "用戶id", required = true, paramType = "path", dataType = "Long") //注意:paramType須要指定爲path,否則不能正常獲取 @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String user(@ApiParam(value = "用戶Id", required = true) @PathVariable Long id) { return "user id :" + id; } /** * 獲取用戶列表 * * @return */ @ApiOperation(value = "獲取用戶列表", notes = "獲取用戶列表") @RequestMapping(value = "", method = RequestMethod.GET) public List list() { List list = new ArrayList(); list.add("Sam1"); list.add("Sam2"); list.add("Sam3"); return list; } /** * 新建用戶 * * @param user * @return */ @ApiOperation(value = "新建用戶", notes = "新建一個用戶") // @ApiImplicitParams({ //注意:paramType須要指定爲body // @ApiImplicitParam(name = "user", value = "用戶數據", required = true, paramType = "body", dataType = "User") // }) @RequestMapping(value = "", method = RequestMethod.POST) public String create(@ApiParam(value = "用戶數據", required = true) @RequestBody User user) { System.out.println("user : " + user.getName() + " " + user.getAge()); return "success 新建user : " + user.getName() + " " + user.getAge(); } /** * 刪除用戶 * * @return */ @ApiOperation(value = "刪除用戶", notes = "經過用戶id刪除用戶") @ApiImplicitParam(name = "id", value = "用戶id", required = true, paramType = "path", dataType = "Long") @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable Long id) { System.out.println("刪除用戶:" + id); return "success 刪除user" + id; } /** * 更新用戶 * * @return */ @ApiOperation(value = "更新用戶", notes = "更新已存在用戶") @ApiImplicitParam(name = "user", value = "用戶數據", required = true, paramType = "body", dataType = "User") @RequestMapping(value = "", method = RequestMethod.PUT) public String update(@RequestBody User user) { System.out.println("更新用戶:" + user.getId() + " " + user.getName() + " " + user.getAge()); return "success 更新user : " + user.getId() + " " + user.getName() + " " + user.getAge(); } }
附:Swagger2相關注解介紹html
@Api:用在類上,說明該類的做用 @ApiOperation:用在方法上,說明方法的做用 @ApiImplicitParams:用在方法上包含一組參數說明 @ApiImplicitParam:用在 @ApiImplicitParams 註解中,指定一個請求參數的各個方面 paramType:參數放在哪一個地方 · header --> 請求參數的獲取:@RequestHeader · query -->請求參數的獲取:@RequestParam · path(用於restful接口)--> 請求參數的獲取:@PathVariable · body(不經常使用) · form(不經常使用) name:參數名 dataType:參數類型 required:參數是否必須傳 value:參數的意思 defaultValue:參數的默認值 @ApiResponses:用於表示一組響應 @ApiResponse:用在@ApiResponses中,通常用於表達一個錯誤的響應信息 code:數字,例如400 message:信息,例如"請求參數沒填好" response:拋出異常的類 @ApiModel:描述一個Model的信息(這種通常用在post建立的時候,使用@RequestBody這樣的場景,請求參數沒法使用@ApiImplicitParam註解進行描述的時候) @ApiModelProperty:描述一個model的屬性
版權聲明:本文爲博主原創文章,轉載請註明出處。java