머리말
"삼키기"의 중국의 의미 인 Swagger. 강력한 API 프레임 워크이며 통합은 매우 간단하며 온라인 문서 검토를 제공 할뿐만 아니라
또한 온라인 문서가 제공됩니다. 또한 Swagger는 편안한 스타일의 API를 쉽게 구축 할 수 있습니다.
Swagger는 OpenAPI 사양을 중심으로 구축 된 오픈 소스 도구 세트로 REST API를 설계, 빌드, 문서화 및 사용하는 데 도움이됩니다.
간단히 말해서, 백그라운드에서 RESTful 인터페이스 테스트를 용이하게하고 백그라운드 인터페이스에있을 때 동적 업데이트를 구현하는 것으로 보입니다.
수정 후 Swagger는 생각대로 테스트를 위해이 인터페이스를 유지할 필요없이 자동 업데이트 할 수 있습니다.
Swagger는 인터페이스가 인터페이스 이름, 요청 메소드, 매개 변수, 반환 정보 등을 포함한 문서를 생성한다는 주석을 통해 주석을 통해 나타납니다.
<pectionency> <groupid> io.springfox </groupid> <artifactid> springfox-swagger2 </artifactid> <버전> 2.7.0 </version> </dependency> <prectionency> <groupid> io.springfox </groupid> <artifactid> springfox-swagger-ui </artifactid>
@Configuration 주석을 통해 구성 클래스임을 나타냅니다.
APIINFO () 구성 몇 가지 기본 정보. APIS ()는 스캔 된 패키지가 문서를 생성하도록 지정합니다.
Createrestapi 함수를 통해 Docket Bean을 작성한 후 APIINFO ()는 API의 기본 정보를 사용합니다 (이 기본 정보는 문서 페이지에 표시됩니다). select () 함수는 ApiseLectorBuilder 인스턴스를 반환하여 디스플레이를 위해 Swagger에 노출되는 인터페이스를 제어합니다. 이 예제는 지정된 스캔 패키지 경로를 사용하여 정의합니다. Swagger는 패키지 아래 컨트롤러에 의해 정의 된 모든 API를 스캔하고 문서 컨텐츠를 생성합니다 (@apiignore가 지정한 요청 제외).
package com.lance.learn.springbootswagger.configuration; import org.springframework.context.annotation.bean; import org.spramframework.context.annotation.configuration; import springfox.documentation.builders.apiinfobuilder; import springfox.documentation.documentations.build Springfox.documentation.builders.requestHandlersElectors; import springfox.documentation.service.apiinfo; import springfox.documentation.service.contact; import springfox.documentation.spi.documentationtype; import springfox.documentation.spring.web.plugins springfox.documentation.swagger2.annotations.enableswagger2;/*** @author lance (zyh)* @functure startup configurat configupar configurn configurat class* @date 2018-07-09 21:24*/ @configuration @swagger2 class swaggerconfiguration {/** ** swagger2 configation for hone con kan con kan kan conger. 스캔 패키지 등과 같은 SWAGGER2 * @return */@Bean Public Docket CreaterestFulapi () {return new Docket (le 인터페이스 address.paths (pathselectors.any ()) .build (); } /** * API 문서의 자세한 정보 기능을 작성하십시오. 주석이 * @return * /private apiinfo apiinfo () {return new ApiinfoBuilder () // page title.title ( "Swagger2") //create.contact ( "lanvetobigdata", "http://www.cnblogs.com/zhangyinhua/", "[email protected]")) // 버전 번호 .version ( "1.0") // description ( "API Description") .Build (); }} 설명은 주로 이름 지정 기능 등에서 나오며 사용자 친화적이지 않습니다. 우리는 일반적으로 문서의 내용을 풍부하게하기 위해 몇 가지 지침을 추가해야합니다.
아래와 같이 @apioperation 주석을 통해 API에 지침을 추가하고 @apiimplicitparams 및 @apiimplicitparam을 사용합니다.
매개 변수에 설명을 추가하는 주석.
1) 예 1
package 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.annotations.annotations.annotations io.swagger.annotations.apioperation; import org.springframework.web.bind.annotation. *; import springfox.documentation.annotations.apiignore; import java.util. *;/** * @author lance (zyh) * @function * @date 2018-09 21:39 */ @ @ @ @ @ @ @ @ @ @ @restcontroller = "/bookcurd") public class bookcontroller {map <long, book> books = collections.synchronizedmap (new Hashmap <long, book> ()); @apioperation (value = "get book list", notes = "book list get") @requestmapping (value = { ""}, method = requestmethod.get) public list <book> getbook () {list <book> book = new arraylist <> (books.values ()); 반환 책; } @apioperation (value = "책 만들기", 노트 = "책 만들기") @apiimplicitparam (name = "book", value = "value ="value = "value ="value ", required = true, dataType ="book ") @RequestMapping (value =" ", methove = requestmethod.post) public string postbook (@requestbody book); "성공"을 반환합니다. } @apioperation (value = "value ="책에 대한 자세한 정보 가져 오기 ", notes ="URL의 ID를 기반으로 세부 정보 가져 오기 ") @apiimplicitparam (name ="id ", value ="id ", required = true, datatype ="long ", paramtype ="path ") @requestmapping (value ="/{id} ", methove problic getod.get.get.get.get.get.get. id) {return books.get (id); } @ApiOperation (value = "업데이트 정보", Notes = "URL의 ID를 기반으로 업데이트 책 정보를 지정하십시오") @apiimplicitParam ({@apiimplicitparam (value = "book id", required = true, dataType = "long", paramtype = "path"), @apiimplicitparam (name = ", value", interity interity " DataType = "book")}) @requestmapping (value = "/{id}", method = requestmethod.put) public String pupuser (@pathvariable long id, @requestbody book) {book book1 = books.get (id); book1.setName (book.getName ()); book1.setPrice (book.getPrice ()); books.put (id, book1); "성공"을 반환합니다. } @apioperation (value = "book", notes = "URL의 ID를 기반으로 책 삭제를 지정) @apiimplicitparam (name ="id ", value ="book id ", required = true, datatype ="long ", paramtype ="path ") @requestmapping (value ="/{id} " deleteuser (@pathvariable long id) {books.remove (id); "성공"을 반환합니다. } @apiignore //이 주석을 사용 하여이 API를 무시하십시오 @RequestMapping (value = "/hi", method = requestMethod.get) public String jsontest () {return "hi you!"; }}2) 예 2
package com.lance.learn.springbootswagger.controller; import com.lance.learn.springbootswagger.bean.user; import io.swagger.annotations.apiimplicitparam; import io.swagger.Annotations.AntOntations.AMPIMPIPITPARAMS; io.swagger.annotations.apioperation; import org.springframework.web.bind.annotation. *; import java.util. *;/** * @author lance (zyh) * @function * @date 2018-07-09 22:00 */ @restcontroller @restcontroller (values = "/user). map <long, user> user = collections.synchronizedMap (new Hashmap <long, user> ()); @apioperation (value = "get get get get", notes = "") @requestmapping (value = { ""}, method = requestmethod.get) public list <useruserlist () {list <user> r = new ArrayList <user> (users.values ()); RETOR R; } @apioperation (value = "value ="supe user ", notes ="사용자 객체에서 사용자 생성 ") @apiimplicitparam (name ="user ", value ="user rementity user ", required = true, dataType ="user ") @RequestMapping (value =" ", method = requestmethod.post) public string postuser (@requestbody user); "성공"을 반환합니다. } @apioperation (value = "value ="사용자 세부 사항 가져 오기 ", notes ="URL의 ID를 기반으로 사용자 세부 사항 가져 오기 ") @apiimplicitparam (name ="id ", value ="user id ", required = true, datatype ="long ") @requestmapping (value ="/{id} ", method = recignsureger (@pathvarivel long varivel) } @apioperation (value = "value ="사용자 세부 사항 ", notes ="URL의 ID를 기반으로 업데이트 객체를 지정하고 전송 된 사용자 정보를 기반으로 사용자 세부 사항을 업데이트하십시오 ") @apiimplicitParam ({@apiimplicitparam (이름 ="id "", value = "user id", required = true, datatype = "long") user ", required = true, datatype ="user ")}) @requestmapping (value ="/{id} ", method = requestmethod.put) public string pupuser (@pathvariable long id, @requestbody user) {user u = new user (); user.put (id, u); "성공"을 반환합니다. } @apioperation (value = "delete user", notes = "URL의 ID에 따라 삭제 삭제를 지정하십시오") @apiimplicitParam (이름 = "id", value = "user id", required = true, datatype = "long") @requestmapping (value = "/{id}", method = requestmethod.Delete) users.remove (id); "성공"을 반환합니다. }}https://github.com/lancetobigdata/springbootlearning/tree/develop/springboot-swagger
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.