您好,登錄后才能下訂單哦!
本文源碼:GitHub·點這里 || GitEE·點這里
整合到Spring Boot中,構建強大RESTful API文檔。省去接口文檔管理工作,修改代碼,自動更新,Swagger2也提供了強大的頁面測試功能來調試RESTful API。
Api:修飾整個類,描述Controller的作用
ApiOperation:描述一個類的一個方法,或者說一個接口
ApiParam:單個參數描述
ApiModel:用對象來接收參數
ApiProperty:用對象接收參數時,描述對象的一個字段
ApiResponse:HTTP響應其中1個描述
ApiResponses:HTTP響應整體描述
ApiIgnore:使用該注解忽略這個API
ApiError :發生錯誤返回的信息
ApiImplicitParam:一個請求參數
ApiImplicitParams:多個請求參數
spring-boot:2.1.3.RELEASE
swagger:2.6.1
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;
/**
* Swagger 配置文件
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.two"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot利用Swagger構建API文檔")
.description("使用RestFul風格, 創建人:知了一笑")
.termsOfServiceUrl("https://github.com/cicadasmile")
.version("version 1.0")
.build();
}
}
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class,args) ;
}
}
(1)、代碼塊
@ApiOperation(value="添加用戶", notes="創建新用戶")
@ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User")
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public ResponseEntity<JsonResult> addUser (@RequestBody User user){
JsonResult result = new JsonResult();
try {
users.put(user.getId(), user);
result.setResult(user.getId());
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
(1)、代碼塊
@ApiOperation(value="用戶列表", notes="查詢用戶列表")
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserList (){
JsonResult result = new JsonResult();
try {
List<User> userList = new ArrayList<>(users.values());
result.setResult(userList);
result.setStatus("200");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
(1)、代碼塊
@ApiOperation(value="用戶查詢", notes="根據ID查詢用戶")
@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer", paramType = "path")
@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){
JsonResult result = new JsonResult();
try {
User user = users.get(id);
result.setResult(user);
result.setStatus("200");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
(1)、代碼塊
@ApiOperation(value="更新用戶", notes="根據Id更新用戶信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long",paramType = "path"),
@ApiImplicitParam(name = "user", value = "用戶對象user", required = true, dataType = "User")
})
@RequestMapping(value = "/updateById/{id}", method = RequestMethod.PUT)
public ResponseEntity<JsonResult> updateById (@PathVariable("id") Integer id, @RequestBody User user){
JsonResult result = new JsonResult();
try {
User user1 = users.get(id);
user1.setUsername(user.getUsername());
user1.setAge(user.getAge());
users.put(id, user1);
result.setResult(user1);
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
(1)、代碼塊
@ApiOperation(value="刪除用戶", notes="根據id刪除指定用戶")
@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long", paramType = "path")
@RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
public ResponseEntity<JsonResult> deleteById (@PathVariable(value = "id") Integer id){
JsonResult result = new JsonResult();
try {
users.remove(id);
result.setResult(id);
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
GitHub·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。