preface

Swaggerui automatically generates interface documents, eliminating the need to frequently update interface documents to ensure consistency between interface documents and code. Knife4j is an enhanced UI implementation of Springfox-Swagger, providing Java developers with a concise, powerful interface documentation experience when using Swagger. However, knife4J still has a few holes, waiting for the authors to refine it.

start

Directory description

List only files that need to be added or modified

├─ ├─ Java ├─ SRC /main/ Java ├─ SRC /main/ Java ├─ Java ├─ SRC /main/ Java ├─ Java ├─ SRC /main/ Java ├─ Java ├─ ├─ SRC /main/ Java ├─ Java ├─ SRC /main/ Java ├─ Java ├─ SRC /main/ Java ├─ Java ├─ SRC /main/ Java ├─ Java ├─ ├ ─ └ -mlDong-mapperCopy the code

Document describing

  • mldong-common/pom.xml
<! --springboot-web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! --swaggerui-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
</dependency>
<! --knife4j-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
Copy the code

The parent project already has a version number, but a copy is kept here for easy copying

<properties>
    <io.springfox-swagger2.version>2.7.0</io.springfox-swagger2.version>
	<swagger-knife.version>The 2.0.3</swagger-knife.version>
 </properties>
Copy the code
  • mldong-common/src/main/java/com/mldong/common/config/SwaggerConfig.java
package com.mldong.common.config;

import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import com.google.common.collect.Lists;

@Configuration
@EnableSwagger2
@Profile({"dev"."test"}) // Only the development environment and test environment will take effect
public class SwaggerConfig implements WebMvcConfigurer {

	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// This is the default swaggerui
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // This is knife4j UI
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
	
    @Bean
    public Docket adminApi(a) {
    	return new Docket(DocumentationType.SWAGGER_2)
    		.groupName("Background Business Interface")
			.apiInfo(adminInfo())
            .select()
            //.apis(RequestHandlerSelectors.basePackage("com.mldong.modules"))
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(security());
    }
    
    private ApiInfo adminInfo(a) {
    	return new ApiInfoBuilder()
        .title("Interface Documentation - Background Management Interface")
        .description("All requests use request body JSON -POST")
        .version("1.0")
        .build();
    }
    private List<ApiKey> security(a) {
    	-header[" auth-token "]
    	return Lists.newArrayList(
    			new ApiKey("Auth-Token"."Auth-Token"."header")); }}Copy the code
  • mldong-admin/src/main/java/com/mldong/modules/sys/controller/SysUserController.java
package com.mldong.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.mldong.modules.sys.entity.SysUser;
import com.mldong.modules.sys.service.SysUserService;

@RestController
@RequestMapping("/sys/user")
@Api(tags="Sys - User Management")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	/** * Add user *@param param
	 * @return* /
	@PostMapping("save")
	@ApiOperation(value="Add user", notes="Add user")
	public int save(@RequestBody SysUser param) {
		return sysUserService.save(param);
	}
	/** * Update user *@param param
	 * @return* /
	@PostMapping("update")
	@ApiOperation(value="Update user", notes="Update user")
	public int update(@RequestBody SysUser param) {
		return sysUserService.update(param);
	}
	/** * Delete user *@param param
	 * @return* /
	@PostMapping("delete")
	@ApiOperation(value="Delete user", notes="Delete user")
	public int delete(@ApiParam(value="User id")Long id) {
		return sysUserService.delete(id);
	}
	/** * Get user * by id@param param
	 * @return* /
	@GetMapping("get")
	@ApiOperation(value="Get user by ID", notes="Get user by ID")
	public SysUser get(@ApiParam(value="User id",required=true)Long id) {
		return sysUserService.get(id);
	}
	/** ** page query user list *@param param
	 * @return* /
	@GetMapping("list")
	@ApiOperation(value="Paging query user list", notes="Paging query user list")
	public Page<SysUser> list(SysUser param, @ApiParam(value="Page n, default 1")@RequestParam(defaultValue="1")Integer pageNum, @ApiParam(value="Page size, default 10")@RequestParam(defaultValue="10")int pageSize) {
		returnsysUserService.list(param, pageNum, pageSize); }}Copy the code

Start running the project

MldongAdminApplication.java

Right-click ->Run As -> Java Application

To access the page

  • Swaggerui document

http://localhost:18080/swagger-ui.html

  • Knife4j document

http://localhost:18080/doc.html

If the project starts normally, you will see the corresponding interface document when you visit the page.

Screenshot, omitted.

Swaggerui commonly used annotations

annotations scope instructions
@API (tags=”sys- User Management “) class Indicates that this class is a swagger resource
@apiOperation (value=” add user “, notes=” add user “) methods Represents an HTTP request operation
@ ApiParam (value = “user id”, the required = true) Used for methods, parameters, and field descriptions Indicates the addition of metadata to the parameter (description, mandatory, etc.)
@apiModel (value=” user entity “) class Represents a description of the class for parameters received by the entity class
@apiModelProperty (value=” username “) For methods, fields Represents a description or data manipulation change to a Model property
@ApiIgnore() For classes, methods, method parameters Indicates that the method or class is ignored
@ApiImplicitParam() Method is used to Represents a single request parameter
@ApiImplicitParams() Method is used to Contains multiple @apiIMPLicitParam

Project source code address

  • The back-end

Gitee.com/mldong/mldo…

  • The front end

Gitee.com/mldong/mldo…

Related articles

Create a suitable for their own rapid development framework – the pilot

Build a suitable for their own rapid development framework – back-end scaffolding

Build a fast development framework for yourself – integrated Mapper