An import-dependent swagger2
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
Second, the configuration file config
package com.offcn.springrestfuldemo001.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration EnableSwagger2 @ // open document public class SwaggerConfig { // 1. declare property api documentation builder Private ApiInfo apiInfo () { return new new ApiInfoBuilder (). Title ( " springboot use online documentation to build RestFul style Apis " ) .description("小demo").termsOfServiceUrl("http://baidu.com/") .contact("java") .version("1.0").build(); } // 2. Configuration core configuration information public Docket No. createRestApi () { return new new Docket No. (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select().apis(RequestHandlerSelectors.basePackage("com.offcn.springrestfuldemo001.controller")) .paths(PathSelectors.any()).build(); } }
Third, the write controller
API to add a description to a comment by @ApiOperation
Api interface method used on the class, the main attributes value (interface name), Notes (Notes), hidden (if hidden), httpMethod, ignoreJsonView, response, responseHeaders the like, can automatically identify certain properties annotation, no configuration.
To add a description to the parameters @ ApiImplicitParams, @ ApiImplicitParam comments
Using interface method Api class of interface parameters will be described, @ ApiImplicitParams only one attribute value, @ ApiImplicitParam The main attributes name (parameter name), value (parameter description), required (if required), dataType (data type) , paramType (parameter type), readOnly (read only) and the like.
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; @RestController @RequestMapping("/users-test") public class UserController { private List<User> listUser= Collections.synchronizedList(new ArrayList<User>()); /** * Updates the specified user id information * @param id * @param user * @return */ @PutMapping("/{id}") @ApiOperation (value = "update the specified user id information", notes = "user information is updated based on the id") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long"), @ApiImplicitParam(name = "user",value = "用户实体user",required = true,dataType = "User") }) public String updateUser(@PathVariable("id") Long id,User user) { for (User user2 : listUser) { if(user2.getId()==id) { user2.setName(user.getName()); user2.setAge(user.getAge()); } } return "success"; } /*** * Delete the specified user id * @param id * @return */ @DeleteMapping("/{id}") @ApiOperation (value = "delete the specified user id information", notes = "delete user information in accordance with id") @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long") public String deleteUser(@PathVariable("id") Long id) { listUser.remove(getUser(id)); return "success"; } }
Fourth, the main startup class
Start the application, and then access the address: HTTP: // localhost: 8080 / Swagger-ui.html
package com.offcn.springrestfuldemo001; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringRestfulDemo001Application { public static void main(String[] args) { SpringApplication.run(SpringRestfulDemo001Application.class, args); } }