Swaggerは、Restful Interface Document Automatic Generation + Functional Testing Functionソフトウェアです。この記事では、Swaggerをプロジェクトに統合する方法といくつかの一般的な問題を簡単に紹介します。プロジェクトソースコードを詳細に分析し、詳細を確認する場合は、参照資料を参照してください。
Swaggerは、RestfulスタイルのWebサービスを生成、説明、呼び出し、視覚化するための標準化された完全なフレームワークです。全体的な目標は、サーバーと同じ速度でクライアントとファイルシステムを更新することです。ファイルメソッド、パラメーター、およびモデルは、サーバー側のコードにしっかりと統合されているため、APIは常に同期し続けることができます。 Swaggerは、強力なAPIを展開および管理し、使用するのが容易になりました。
レンガを動かす学生にとって、インターフェイスを書くのは簡単で、インターフェイスドキュメントを書くのは非常に迷惑です。インターフェイスが変更された場合、インターフェイスドキュメントの維持はさらに迷惑であるため、ドキュメントがプログラムと一致しないことがよくあります。
しばらくすると、開発者でさえ混乱しました
Swagger2は、上記の問題を迅速かつ便利に解決します。 Spring MVCプログラムで強力なRestful APIドキュメントを整理できる新しいお気に入り。
次のコードが直接追加されます
pom.xml
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modeleversion> 4.0.0 </modelversion> <グループ<グループ<グループ<</groupid> <artifactid> wealthweb </artifactid> <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> wealthweb </name> <説明> spring boot </description> <parent> org.springframework.boot < <バージョン> 1.5.9.Release </version> <RecationPath/> <! - リポジトリからの親を探してください - > </parent> <Properties> <propert.build.sourceencoding> utf-8 </project.build.sourceding> <project.reporting.outputencoding <Java.version> 1.8 </java.version> </properties> <dependencies> <dependency> springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <dependency> <グループ< <artifactid> spring-boot-starter-test </artifactid> <scope> test </scope> </dependency> <dependency> <groupid> io.springfox </groupid> <artifactid> springfox-swagger2 </artifactid> <バージョン> 2.7.0 </バージョン<artifactid> springfox-swagger-ui </artifactid> <バージョン> 2.7.0 </version> </dependency> </dependencies> </project>
構成クラスを作成します
パッケージcom.zhongxin.wealth.apiconfig; import org.springframework.context.annotation.bean; Import org.springframework.context.annotation.configuration; Import springfox.documentation.builders.apiinfobuilder; import spring.documentation.builders. springfox.documentation.builders.requesthandlerselectors; Import springfox.documentation.service.apiinfo; import springfox.documentation.spi.documentationtype; Import springfox.document.spring.web.plugins.docket; 2017/12/8のDingysによる。 */@configuration@enableswagger2public class swagger2 {@bean public docket createrestapi(){return new docket(documentationtype.swagger_2).apiinfo().select().apis(requesthandlerselectors.basepackage( " .paths(pathselectors.any())).build(); } private apiinfo apiinfo(){return new apiinfobuilder().title( "langfangパブリックローンビッグデータ統計結果インターフェイス").version( "1.0").build(); }}コントローラーを書き込みます
パッケージcom.zhongxin.wealth.weach; import io.swagher.annotations.apioperation; Import org.springframework.web.bind.annotation.Reg.SpringFramework.web.bind.annotation.RequestMethod; Import org.springframework.web.bind.not. 2017/12/7にDingysによって作成されました。 */ @retscontroller @requestMapping( "/hello")パブリッククラスHellowordController {@Apioperation(value = "Test Interface"、notes = "これは、ビジネスロジックなしでコントローラーコールをテストするための単なるインターフェイスです")@requestmapping(value = {{"/test"}、requestmeth.get test hello() }}コードが完了し、効果が準備ができています
[試してみてください]をクリックしてください!
それは非常に詳細で、非常にハイエンドですか?
注:統合プロセス中に使用したばかりのSwagger2.2.2バージョンには、ホームページにエラーリマインダーが表示されます。
{"schemavalidationmessages":[{"level": "error"、 "message": "ファイルから読めないhttp://127.0.0.1:8888/v2/api-docs"}]}ただし、ブラウザアクセス:http://127.0.0.1:8888/v2/api-docsは再び結果を得ることができます
{"swagger": "2.0"、 "info":{"version": "1.0"、 "title": "langfang commission-loanビッグデータ統計結果出力interface "、" contact ":{}、" license ":{}}、" host ":" "127.0.0.1:88888"、" basepath":"/"tags ": ["name"hello-word-controller","description":"hello wordコントローラー "}]、" paths ":{"/hello/test ":{" get ":{" tags ":[" hello-word-controller "]、" summary ":" test interface "、" description ":"これは、ビジネスロジックなしでコントローラーコールをテストするための単なるインターフェイスです。 ""/"]:["/"]、" responses ":{" 200 ":{" description ":" ok "、" schema ":{" type ":" string "}}、" 401 ":{" description ":" unauthorized "}、" 403 ":{" ":" "": "404": "" ":「見つかった"}}}}}}}特定の理由は不明であり、2.7.0バージョンの後には表示されていません。
要約します
上記は、Springboot Integrated Swaggerが紹介した例のコードです。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!