免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
1 Swagger2はじめに
私たちはそれ手書きのAPIドキュメントの経験を持っていたと信じて、アップあまりにも非効率的な、企業は一般的にこの分野でのニーズを持っていますが、手書きのAPIドキュメントは、非常に深刻な問題を抱えています!この問題を解決するために、Swagger2が判明しました。Swagger2は容易インターフェースの背景安らか形態、動的更新をテストすることができます。我々はインターフェイスの背景に変更された場合には、Swagger2を自動的に更新することができます。
2依存性を追加します。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3つのSwagger2の設定クラス
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("edu.szu.test.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("使用swagger2构建的api文档")
.description("点我关注 https://blog.csdn.net/Geffin")
.termsOfServiceUrl("https://blog.csdn.net/Geffin")
.version("1.0")
.build();
}
}
4起動クラスのコンフィグレーション
@SpringBootApplication
@MapperScan("edu.szu.test.mapper")
@EnableSwagger2
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
EnableSwagger2注釈起動クラス@追加するには注意してください
5コントローラファイルを作成します。
@RestController
@Api(tags="测试接口模块")//@Api:用在请求的类上,表示对类的说明
@RequestMapping("/test")
public class BookController {
@Autowired
BookService bookService;
// 创建线程安全的Map
static Map<Integer, Book> map = Collections.synchronizedMap(new HashMap<Integer, Book>());
//根据id查询书籍信息
//@ApiOperation:用在请求的方法上,说明方法的用途、作用,value表示方法的用途,notes为方法的备注
@ApiOperation(value="根据id查询书籍信息", notes="根据id查询书籍信息")
//@ApiImplicitParam:用在请求的方法上,表示参数说明,name表示参数名,value为参数的说明,required表示参数是否必须传,paramType表示参数放在哪个地方,dataType表示参数类型
@ApiImplicitParam(name = "id", value = "书籍ID", required = true, dataType = "int",paramType = "path")
@RequestMapping(value = "/selectById/{id}",method = RequestMethod.GET)
public ResponseEntity<JsonResult> selectById (@PathVariable Integer id){
JsonResult r = new JsonResult();
try {
Book book = bookService.selectById(id);
r.setResult(book);
r.setStatus("ok");
} catch (Exception e) {
r.setResult(e.getClass().getName() + ":" + e.getMessage());
r.setStatus("error");
e.printStackTrace();
}
return ResponseEntity.ok(r);
}
//添加书籍
@ApiOperation(value="添加书籍", notes="添加书籍")
@ApiImplicitParam(name = "book", value = "书籍", required = true, dataType = "Book")
@RequestMapping(value = "/insert",method = RequestMethod.POST)
public ResponseEntity<JsonResult> insert (@RequestBody Book book){
JsonResult r = new JsonResult();
try {
bookService.insert(book);
r.setResult(book.getId());
r.setStatus("ok");
} catch (Exception e) {
r.setResult(e.getClass().getName() + ":" + e.getMessage());
r.setStatus("error");
e.printStackTrace();
}
return ResponseEntity.ok(r);
}
//根据id删除书籍
@ApiOperation(value="删除书籍", notes="删除书籍")
@ApiImplicitParam(name = "id", value = "书籍id", required = true, dataType = "int", paramType = "path")
@RequestMapping(value = "/deleteById/{id}",method = RequestMethod.DELETE)
public ResponseEntity<JsonResult> deleteById (@PathVariable Integer id){
JsonResult r = new JsonResult();
try {
bookService.deleteById(id);
r.setResult(id);
r.setStatus("ok");
} catch (Exception e) {
r.setResult(e.getClass().getName() + ":" + e.getMessage());
r.setStatus("error");
e.printStackTrace();
}
return ResponseEntity.ok(r);
}
//修改书籍信息
@ApiOperation(value="修改书籍信息", notes="修改书籍信息")
@ApiImplicitParam(name = "book", value = "书籍", required = true, dataType = "Book")
@RequestMapping(value = "/update",method = RequestMethod.PUT)
public ResponseEntity<JsonResult> update (@RequestBody Book book){
JsonResult r = new JsonResult();
try {
bookService.update(book);
r.setResult(book);
r.setStatus("ok");
} catch (Exception e) {
r.setResult(e.getClass().getName() + ":" + e.getMessage());
r.setStatus("error");
e.printStackTrace();
}
return ResponseEntity.ok(r);
}
}
注釈の一部:
- @Api:要求されたクラスで使用される、クラスの説明
- @ApiOperation:方法、効果の使用は、値は方法の使用を表す要求に使用される方法、発言注方法
- @ApiImplicitParam:リクエストに使用される方法は、パラメータの説明を発現し、名前は、パラメータの記述としてパラメータ名、値を表し、必要なパラメータは、代わりにパラメータを表すパス、paramTypeは、データ型のパラメータタイプを示すかどうかを示す必要があります
JSON形式の出力クラスIはするJsonResultを使用します。
public class JsonResult {
private String status = null;
private Object result = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
6プロジェクト構造
次のように私のプロジェクトが構成されています
7ビューSwagger2ドキュメント
SpringBootプロジェクトを開始し、訪問にhttp:// localhost:8080 /威張っ-ui.html
8テスト機能を使用します
それが成功を返すことを観察しました
私達のデータベースを開き、データが正常にSwagger2に統合された春ブーツに代わって、正常に挿入されていることがわかりました。