Swaggerは、RestfulスタイルのWebサービスを生成、説明、呼び出し、視覚化するための標準化された完全なフレームワークです。全体的な目標は、サーバーと同じ速度でクライアントとファイルシステムを更新することです。ファイルメソッド、パラメーター、およびモデルは、サーバー側のコードにしっかりと統合されているため、APIは常に同期し続けることができます。
Swaggerは、強力なAPIを展開および管理し、使用するのが容易になりました。わかりました、上記は公式声明であり、私はそれを直接コピーしました。私の意見では、Swaggerはインターフェイスドキュメントマネージャーです。過去には、通常、世界でインターフェイスを書きましたが、テスト時にサードパーティのツールに依存する必要がある問題があります。 GETインターフェイスは大丈夫で、ブラウザによって直接開かれます。投稿は他のツールにのみ依存できます。 Swaggerは、コードの注釈を使用して、インターフェイスドキュメント(Javaee)を直接生成できます。ほとんどの人はこの方法を使用しており、プロジェクトに直接統合されてメンバーが表示されるようになり、直接テストすることもできます。さらに、Swaggerのインターフェースも良いです。たぶんこれが私がSwaggerを使用することを選んだ理由です。役人が直接言及するRESTFUL STYLEは安らかではありません。スタイルインターフェイスも使用できます。もちろん、Swaggerがインターフェイスの命令を手動で書き込む別の方法があります。利点は、コードのみがコードのみを持っていることです。なぜなら、Swaggerインターフェイスアノテーションがコードに追加されると、コードの量が大幅に増加するからです。もちろん、不利な点は、コードを変更した後、インターフェイスドキュメントを変更する必要があることです。
SpringMVCは、Springfox-Swagger2とSpringfox-Swagger-uiを統合するのが非常に簡単で、2つのステップのみです。
(1)POMに依存関係を追加します
<Dependency> GroupId> io.springfox </groupid> <artifactid> springfox-swagger-ui </artifactid> <version> $ {springfox-swagger.version} </version> </dependency> <依存関係> <groupid> io.springfox </groupid> springfugaghtid> springfugaghtid> <バージョン> $ {springfox-swagger.version} </version> </dependency>(2)Swagger構成クラスを追加します:
@configuration @enableSwagger2 @enablewebmvc @componentscan( "com.xxx.controller")パブリッククラスswaggerconfig {}次に、http://localhost/swagger-ui.htmlを介してプロジェクトのすべてのインターフェイス情報を表示でき、http:// localhost/v2/api-docsを介してJSONデータを見ることができます。
しかし、これらのAPIドキュメントを生産環境で無効にするにはどうすればよいですか?私は多くの方法を試しましたが、ついにシンプルで実用的な方法を見つけました。
@configuration @enableSwagger2 @enablewebmvc @componentscan( "com.xxx.controller")パブリッククラスswaggerconfig {@autowired configservice configservice; @bean public docket customdocket(){if(configservice.getServerenv()== serverEnvenum.online){return new Docket(documenttationType.swagher_2).apiinfo(apiinfoonline().select().select().paths().paths().paths()// 。建てる(); } else {return new Docket(documentationType.swagger_2).apiinfo(apiinfo()); }} private apiinfo apiinfo(){return new apiinfobuilder().title( "xxx system").description( "xxx system interface").license( "").licenseurl( "").termsofserviceurl( "")。 。建てる(); } private apiinfo apiinfoonline(){return new apiinfobuilder().title( "").description( "").license( "").licenseurl( "").termsofserviceurl( "").version( "")。 }}ページhttp://localhost/swagger-ui.htmlには引き続きアクセスできますが、http:// localhost/v2/api-docsを含むコンテンツはありません。
より良い方法があるはずです!
参照://www.vevb.com/article/135312.htm
SwaggerはSpringMVCと同じコンテキストでなければなりません。SpringMVCはSpringのサブコンテキストにすぎません。 SwaggerがSpringコンテキストの負荷をかけると、SwaggerのURLをSpringMVCインターセプターと傍受することはできません。
したがって、2つの解決策があります。
注釈を使用する場合:
(1)Spring-MVC構成:
<! - アノテーションを使用してBeanを自動的に登録し、@Controllerのみをスキャンします - > <コンテキスト:Component-ScanSycansaber-Package = "com.inspur.eyun.yunbx" use-default-filters = "false"> <! - 複数と分離する場合、 "、" - > <context:include expression = "org.springframework.stertype.controller"/> <context:include-filter type = "assignable" expression = "com.inspur.eyun.yunbx.swagger.swaggerconfig"/> </context:component-scan>
Swagger構成を追加する必要があることに注意してください。
(2)スプリング構成:
<! - パッケージスキャン、アノテーション関連 - > <コンテキスト:Component-Scanベースパッケージ= "com.inspur.eyun.yunbx"> <context:explude-filter type = "annotation" expression = "org.springframework.steretype.controller"/> <コンテキスト= < expression = "com.inspur.eyun.yunbx.swagger.swaggerconfig"/> </context:component-scan>
Swaggerを除外することに注意してください
(3)Swagger構成:
@configuration @enableSwagger2 @enablewebmvc @componentscan( "com.inspur.eyun.yunbx.controller")パブリッククラスswaggerconfig {}@configurationアノテーションに注意してください。
もちろん、より推奨される方法は、XML構成方法を使用することです。これにより、Swagger依存関係パッケージを導入する必要がないためです。
(1)Spring-MVC構成:
<! - アノテーションを使用してBeanを自動的に登録し、@Controllerのみをスキャンします - > <コンテキスト:Component-ScanSycansaber-Package = "com.inspur.eyun.yunbx" use-default-filters = "false"> <! - 複数と分離する場合、 "、" - > <context:include expression = "org.springframework.stereotype.controller"/> </context:component-scan> <import resource = "classpath:spring-mvc-swagger.xml"/>
spring-mvc-swagger.xml:
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-inst" xsis " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spigns.xsd構成、生産環境空 - > <bean /> < /bean>
注:Swaggerを構成ファイルに個別に配置します。オンライン環境の場合、ファイルコンテンツは空です。オフラインテスト環境の場合、Swaggerが構成されています。
(2)スプリング構成:
<! - パッケージスキャン、アノテーション関連 - > <コンテキスト:Component-Scanベースパッケージ= "com.inspur.eyun.yunbx"> <context:exclude-filter type = "annotation" expression = "org.springframework.stereotype.controller"/>> </component-scan>> </
(3)Swagger構成:
@enableSwagger2 @enablewebmvc public class swaggerconfig {@bean public docket customdocket(){return new docket(documentationType.swagger_2).apiinfo().select().apis(requesthandlerselectors.basepackage( .paths(pathselectors.any()).build(); } private apiinfo apiinfo(){return new apiinfobuilder().title( "xxx platform").description( "xxxプラットフォームインターフェイス").license( "").licenseurl( "").termsofserviceurl( "").version( "1.0.0").contact( "" "" ").contact(" "" ").contact(" "" ").contact(" 。建てる(); }}注:ここでは、@Configurationを削除し、同時にPOMを変更し、複数のプロファイルパッケージを構成しました。
pom.xml:
<! - swagger-> <dependency> <groupid> io.springfox </groupid> <artifactid> springfox-swagger2 </artifactid> <version> $ {springfox-swagger.version} </version> <scope> $ {swagher.scope} </scope> <artifactid> springfox-swagger-ui </artifactid> <scope> $ {swagger.scope} </scope> <version> $ {springfox-swagger-ui.version} </version> </dependency>注:ここで依存するスコープは動的に設定されています。オンライン環境の場合は、提供されるスコープを設定できます。
<プロファイル> <profile> <id> dev </id> <properties> <profiles.active> dev </profiles.active> <swagger.scope> compile </swagger.scope> </properties> <activebydefault> true </activebydefault> </activation> </</<id> test </id> <> <> <proptionies> <Profiles.active> test </profiles.active> <swagger.scope>コンパイル</swagger.scope> </properties> </profile> <profile> <id> online </id> <プロパティ> <プロパティ> <プロファイル>テスト>テスト</profiles.active> <swagger.scope>コンパイル<properties> <profiles.active> online </profiles.active> <swagger.scope>提供
さまざまなプロファイルを介して、Swaggerの依存関係のさまざまなスコープを設定します!
注:Springfox-Swagger.version = 2.7.0にはバグがあり、下位バージョン2.6.1を使用できます。あまりにもクソトリック!
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。