- 1.
pom.xml adding Swagger2 dependency in the
1 <!--join Swagger2 's dependency -2 <Dependency>3 <groupId>Io.springfox</groupId>4 <Artifactid>Springfox-swagger2</Artifactid>5 <version>2.2.2</version>6 </Dependency>7 <Dependency>8 <groupId>Io.springfox</groupId>9 <Artifactid>Springfox-swagger-ui</Artifactid>Ten <version>2.2.2</version> One </Dependency>
- 2.
Application.java Create a Swagger2 configuration class at the siblingSwagger2
1 ImportOrg.springframework.context.annotation.Bean;2 Importorg.springframework.context.annotation.Configuration;3 ImportSpringfox.documentation.builders.ApiInfoBuilder;4 Importspringfox.documentation.builders.PathSelectors;5 Importspringfox.documentation.builders.RequestHandlerSelectors;6 ImportSpringfox.documentation.service.ApiInfo;7 ImportSpringfox.documentation.spi.DocumentationType;8 ImportSpringfox.documentation.spring.web.plugins.Docket;9 ImportSpringfox.documentation.swagger2.annotations.EnableSwagger2;Ten One A@Configuration//let spring load the class configuration -@EnableSwagger2//Enable Swagger2 - Public classSwagger2 { the - @Bean - PublicDocket Createrestapi () { - return NewDocket (documentationtype.swagger_2) +. Apiinfo (Apiinfo ())//basic information used to create the API (these basic information is now available on the document page) - . Select () + //used to control which interfaces are exposed to swagger to show, A //This example is defined by the package path of the specified scan, and swagger scans all the controller-defined APIs under the package and produces the document content at //(except for requests specified by @apiignore) -. APIs (Requesthandlerselectors.basepackage ("Com.example.demo1.web")) - . Paths (Pathselectors.any ()) - . Build (); - } - in Privateapiinfo Apiinfo () { - return NewApiinfobuilder () to. title (using Swagger2 to build restful APIs in Spring boot)) +. Description ("More Spring boot related articles please follow: https://www.cnblogs.com/yangjiming/category/1240129.html") -. Termsofserviceurl ("http://www.cnblogs.com/yangjiming/") the. Contact ("Mr.yang") *. Version ("1.0") $ . Build ();Panax Notoginseng } -}
- 3. Add the contents of the document to the code by @ApiOperation annotations To add instructions to the API, through @ApiImplicitParams,@ApiImplicitParam annotation To add a description to the parameter.
1 ImportCom.example.demo1.domain.User;2 ImportIo.swagger.annotations.ApiImplicitParam;3 ImportIo.swagger.annotations.ApiImplicitParams;4 Importio.swagger.annotations.ApiOperation;5 Importorg.springframework.web.bind.annotation.*;6 7 ImportJava.util.*;8 9 @RestControllerTen@RequestMapping (value= "/users")//with this configuration, the following mappings are under/users One Public classUsercontroller { A - StaticMap<long, user> users = Collections.synchronizedmap (NewHashmap<long, user>()); - the@ApiOperation (value= "Get user list", notes= "") -@RequestMapping (value={""}, method=requestmethod.get) - PublicList<user>getuserlist () { -list<user> r =NewArraylist<user>(Users.values ()); + returnR; - } + A@ApiOperation (value= "Create user", notes= "create user based on user object") at@ApiImplicitParam (name = "User", value = "Client detail entity user", required =true, DataType = "User") -@RequestMapping (value= "", method=requestmethod.post) - PublicString postuser (@RequestBody user user) { - Users.put (User.getid (), user); - return"Success"; - } in -@ApiOperation (value= "Get user Details", notes= "get user details based on the ID of the URL") to@ApiImplicitParam (name = "id", value = "User id", required =true, DataType = "Long", paramtype= "path") +@RequestMapping (value= "/{id}", method=requestmethod.get) - PublicUser getUser (@PathVariable Long id) { the returnusers.get (ID); * } $ Panax Notoginseng@ApiOperation (value= "Update user Details", notes= "specifies the update object based on the ID of the URL and updates the user details based on the passed user information.")) - @ApiImplicitParams ({ the@ApiImplicitParam (name = "id", value = "User id", required =true, DataType = "Long", paramtype= "path"), +@ApiImplicitParam (name = "User", value = "Client detail entity user", required =true, DataType = "User") A }) the@RequestMapping (value= "/{id}", method=requestmethod.put) + PublicString putuser (@PathVariable Long ID, @RequestBody user user) { -User U =users.get (ID); $ U.setname (User.getname ()); $ U.setage (User.getage ()); - users.put (ID, u); - return"Success"; the } - Wuyi@ApiOperation (value= "Delete user", notes= "Specify the Delete object based on the ID of the URL") the@ApiImplicitParam (name = "id", value = "User id", required =true, DataType = "Long", paramtype= "path") -@RequestMapping (value= "/{id}", method=requestmethod.delete) Wu PublicString deleteuser (@PathVariable Long id) { - users.remove (ID); About return"Success"; $ } -}
1 Public classUser {2 3 PrivateLong ID;4 PrivateString name;5 PrivateInteger age;6 7 8 PublicLong getId () {9 returnID;Ten } One A PublicString GetName () { - returnname; - } the - PublicInteger getage () { - returnAge ; - } + - Public voidsetId (Long id) { + This. ID =ID; A } at - Public voidsetName (String name) { - This. Name =name; - } - - Public voidsetage (Integer age) { in This. Age =Age ; - } to}View Code
- 4. Complete the above code add, start the Spring boot program, visit: http://localhost:8080/swagger-ui.html
- Original Reference Address: http://blog.didispace.com/springbootswagger2/
Spring Boot uses Swagger2