Integrate SpringBoot Swagger2
Add the dependent
< the dependency > < groupId > IO. Springfox < / groupId > < artifactId > springfox - swagger2 < / artifactId > < version > 2.9.2 < / version > </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> The < version > 2.9.2 < / version > < / dependency >Copy the code
Add the Swagger configuration class
Add @enablesWagger2 to the config class to indicate that Swagger is enabled. Inject a Docket class to configure some API information. The apiInfo() method defines several document messages as follows:
@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket (DocumentationType SWAGGER_2). ApiInfo (apiInfo ()). The select () / / for the current controller to generate the API documentation package Apis (RequestHandlerSelectors. BasePackage (com. Xx. "controller")) / / for the controller to generate the Api documentation with @ Api annotations / / Apis (RequestHandlerSelectors. WithClassAnnotation (Api. Class)) / / for @ ApiOperation annotation methods to generate the Api documentation / / .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo ApiInfo () {return new ApiInfoBuilder().title("SwaggerUI demo ") // Contact(new contact(" WWW ", "https://www.hyms.com", "[email protected]")) // version number.version("1.0").build(); }}Copy the code
Writing API documentation
Create a User entity class under the domain package and use the @APIModel annotation to indicate that this is an entity returned by Swagger. The @APIModelProperty annotation indicates several attributes of the entity as follows (getters/setters omitted) :
@apiModel (value = "userid ", description =" userid ") public class User {@apiModelProperty (value = "userid ", hidden = true) private Long id; @APIModelProperty (value = "username ") private String name; @APIModelProperty (value = "user age ") private String age; // getter/setter }Copy the code
Finally, create a UserController class under the Controller package that provides the user API as follows:
@restController @requestMapping ("/users") @API (tags = "UserController ") public class UserController {Map<Long, User> users = Collections.synchronizedMap(new HashMap<>()); @getMapping ("/") @apiOperation (value = "Get user list ", Public List<User> getUserList() {return new ArrayList<>(users.values()); } @postmapping ("/") @apiOperation (value = "create User ") public String addUser(@requestBody User User) { users.put(user.getId(), user); return "success"; } @getMapping ("/{id}") @apiOperation (value = "user id") @apiImplICITParam (name = "id", value =" user ID ", paramType = "query", dataTypeClass = Long.class, defaultValue = "999", required = true) public User getUserById(@PathVariable Long id) { return users.get(id); } @putMapping ("/{id}") @apiOperation (value = "update user by ID ") @apiImplICITParams ({@apiIMPLICITParam (name = "id", Value = "user id", @apiImplICITParam (name = "name", value =" user name", defaultValue = "wupx"), @apiIMPLICITParam (name = "age", value = "user age", defaultValue = "18") }) public User updateUserById(@PathVariable Long id, @RequestParam String name, @RequestParam Integer age) { User user = users.get(id); user.setName(name); user.setAge(age); return user; } @deletemapping ("/{id}") @apiOperation (value = "delete user ", notes =" delete user by id") @apiimplicitParam (name = "id", Value = "user ID ", dataTypeClass = long. class, required = true) public String deleteUserById(@PathVariable Long id) { users.remove(id); return "success"; }}Copy the code
Start the project, visit http://localhost:8080/swagger-ui.html, you can see we define the document has been displayed on the page Swagger
More interview information, JDK documents, Alibaba Java development manual, PDF books, videos, public number “Java road”