序文
スワッガー、「ツバメ」の中国の意味。これは強力なAPIフレームワークであり、その統合は非常に簡単であり、オンラインドキュメンテーションレビューを提供するだけでなく、
また、オンラインドキュメントが提供されています。さらに、Swaggerは安らかなスタイルのAPIを簡単に構築できます。
Swaggerは、Openapi仕様の周りに構築された一連のオープンソースツールで、REST APIの設計、構築、文書化、使用を支援します。
簡単に言えば、バックグラウンドでRESTFULインターフェイスのテストを容易にし、バックグラウンドインターフェイスにあるときに動的更新を実装するように見えます。
変更後、Swaggerは、このインターフェイスをテスト用に維持する必要なく、自動的に更新できます。
Swaggerは、インターフェイスがインターフェイス名、要求方法、パラメーター、返信情報などを含むドキュメントを生成することを注釈を通して示します。
<Dependency> groupId> io.springfox </groupid> <artifactid> springfox-swagger2 </artifactid> <version> 2.7.0 </version> </dependency> <dependency> <groupid> io.springfox </groupid> <artifactid> springfox-swox-verion </artificid>
@Configuration Annotationを通じて、それが構成クラスであることを示し、 @enableSwagger2はSwagger2を有効にします。
APIINFO()構成いくつかの基本情報。 API()は、スキャンされたパッケージがドキュメントを生成することを指定します。
Createrestapi関数を介してドケットBeanを作成した後、APIINFO()はAPIの基本情報を使用します(これらの基本情報はドキュメントページに表示されます)。 select()関数は、ApiselectorBuilderインスタンスを返して、どのインターフェイスがディスプレイのためにSwaggerに露出しているかを制御します。この例では、指定されたスキャンされたパッケージパスを使用して定義します。 Swaggerは、パッケージの下でコントローラーによって定義されたすべてのAPIをスキャンし、ドキュメントコンテンツを生成します(@Apiignoreで指定された要求を除く)。
パッケージcom.lance.learn.springbootswagger.configuration; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; Import springfox.documentation.builders.apiinfobuilderder; Importfox.documentation springfox.documentation.builders.requesthandlerselectors; Import springfox.documentation.service.apiinfo; Import springfox.documentation.service.contact; import springfox.documentation.spi.documentationtype; Import springfox.documentation springfox.documentation.swagher2.annotations.enableswagger2;/*** @author lance(zyh)* @function swagger startup configuration class* @date 2018-07-09 21:24*/ @configuration @enableSwagger2public class swaggerconfiguration {/** swagher2 configation file2など、swagher2 swaght file2、パッケージなど * @return */@bean public docket createrestfulapi(){return new docket(documentationType.swagher_2).pathmapping( "/").apis(requesthandlerselectors.basepackage( "com.lance.lance.learn.springbootswaggegagger.controller")/ext the paths the path the paths ext the paths address.paths(pathselectors.any()).build(); } /** * APIドキュメントの詳細情報関数を作成します。注釈が * @return * /private apiinfo apiinfo(){return new apiinfobuilder()// page tittle.title( "Swagge2を使用して安らかなAPIを構築するスプリングブートテスト) "http://www.cnblogs.com/zhangyinhua/"、 "[email protected]") }}説明は主に機能の命名などからのものであり、ユーザーフレンドリーではありません。通常、ドキュメントのコンテンツを豊かにするための指示を追加する必要があります。
以下に示すように、@Apioperationアノテーションを介してAPIに手順を追加し、@APIIMPLICTPARAMSと@APIIMPLITITPARAMを使用します
パラメーターに説明を追加するための注釈。
1)例1
パッケージcom.lance.learn.springbootswagger.controller; import com.lance.learn.springbootswagger.bean.book; Import io.swagger.annotations.apiimplicitparam; Import io.swagger.annotations.apiimplicitparams; Import io.swagger.annotations.apimplicticparams; Import.Apimplicticparms io.swagger.annotations.apioperation; import org.springframework.web.bind.annotation。 *; Import springfox.documentation.annotations.Annotations.apiignore;/** * @author lance(zyh) * @function * @date 2018-07-09 21:39 */ @requinter "/bookcurd")public class bookcontroller {map <long、book> books = collections.synchronizedMap(new hashmap <long、book>()); @apioperation(value = "get book list"、notes = "get book list")@requestmapping(value = {"}、requestmethod.get)public list <book> getbook(){list <book> book = new arraylist <>(books.values());戻る本; } @apioperation(value = "create a book"、notes = "book")@apiimplictparam(name = "book"、value = "book detailing entity"、rebys = true、datacype = "book")@requestmapping(value = ""、method = requestmethod.post)public string postbook(@requestbody book){book); book(); 「成功」を返します。 } @apioperation(value = "本の詳細情報を取得"、notes = "urlのIDに基づいて詳細情報を取得)@apiimplicitparam(name =" id "、value =" id "、必須=" long "、paramtype =" path ")@requestmapping(value =" {return books.get(id); } @Apioperation(value = "Update Information"、Notes = "URLのIDに基づいてブック情報を更新する")@apiimplicitparams({@apiimplicitparams(name = "id"、value = "book id"、evenive = true、true、true、true、true、true、datatepe = "long"、ladtype = "path") datatype = "book")})@requestmapping(value = "/{id}"、method = requestmethod.put)public string putuser( @pathvariable long id、 @requestbody book book){book books.get(id); book1.setname(book.getname()); book1.setprice(book.getprice()); books.put(id、book1); 「成功」を返します。 } @apioperation(value = "delete book"、notes = "urlのIDに基づいて本の削除を指定)@apiimplicitparam(name =" id "、value =" book id "、exicling =" long "、paramtype =" path ")@requestmapping(value =" deleteuser(@pathvariable long id){books.remove(id); 「成功」を返します。 } @apiignore //このアノテーションを使用して、このapi @requestmapping(value = "/hi"、method = requestmethod.get)public string jsontest(){return "hi you!"; }}2)例2
パッケージcom.lance.learn.springbootswagger.controller; import com.lance.learn.springbootswagger.bean.user; Import io.swagger.annotations.apiimplicitparam; Import io.swagger.annotations.apiimplicitparams; io.swagge.annotations.apigmplictapparams; Import io.Annotations.apimplictapparams; io.swagger.annotations.apioperation; import org.springframework.web.bind.annotation。 *; Import java.util。 *;/** * @author lance(zyh) * @function * @date 2018-07-09 22:00 */ @rectcontroller @requestmaptin map <long、user> users = collections.synchronizedMap(new hashmap <long、user>()); @apioperation(value = "get user list"、notes = "")@requestmapping(value = {""}、method = requestmethod.get)public list <user> getuserlist(){list <user> r = new arraylist <users <users.(users.values()); rを返します。 } @apioperation(value = "create user"、notes = "ユーザーオブジェクトからユーザーを作成する")@apiimplicitparam(name = "user"、value = "user detterming entity user"、rebys = true、datatype = "user")@requestmapping(value = ""、method = requestmethod.post)postuser(ユーザー)、ユーザー(ユーザー) 「成功」を返します。 } @apioperation(value = "ユーザーの詳細を取得"、notes = "URLのIDに基づいてユーザーの詳細を取得)@apiimplictparam(name =" id "、value =" user id "、rebys = true、long")@requestmapping(value = "/{id}"、requestmethod.get) } @APIOPERATION(value = "ユーザーの詳細を更新する"、notes = "URLのIDに基づいてオブジェクトを更新し、送信されたユーザー情報に基づいてユーザーの詳細を更新する")@apiimplicitparams(@apiimplicitparams(name = "id"、value = "user id"、evens = true、datatype = "long") user "、必須= true、dataType =" user ")})@requestMapping(value ="/{id} "、method = requestmethod.put)public string putuser( @pathvariable long id、 @requestbody user user){user u = new user(); users.put(id、u); 「成功」を返します。 } @apioperation(value = "delete user"、notes = "urlのIDに従って削除オブジェクトを指定)@apiimplictparam(name =" id "、value =" user id "、rebys = dataType =" long ")@requestmapping(value ="/{id} " users.remove(id); 「成功」を返します。 }}https://github.com/lancetobigdata/springbootlearning/tree/develop/springboot-swagger
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。