Introduction:

The author is now a Java background development engineer, responsible for the implementation of background business code in the company. The company is front and back end development mode, which requires background personnel to write interface, front-end personnel call. This requires an interface documentation that can show front-end developers the interfaces written at the back end in real time. So far I know of two kinds:

  • Swagger
  • Knife4j: Knife4j is an enhanced solution for Java MVC framework integration with Swagger Api documentation. Formerly swagger-Bootstrap-UI, Knife4j was named for being as small, lightweight, and powerful as a dagger!

Both can do the same thing.

1 Swagger

1.1 Adding a Dependency

Add Swagger dependencies to the POM.xml file before using it

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>
Copy the code

The SpringBoot project created by default has spring-boot-starter by default. In addition, you need to import the spring-boot-starter-web dependency of the Web module.

1.2 Adding the Configuration

  • Start by adding the following code to application.yml
swagger2:
  enable: true
Copy the code
  • Write the Swagger configuration class
package com.ifknow.config;

import com.ifknow.annotation.Api_Base;
import com.ifknow.annotation.Api_Business;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/ * * *@Author: ifknow <br>
 * @Date: 2020/8/20  11:19 <br>
 * @Description: Swagger2 Configuration class */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Value("${swagger2.enable}")
    private boolean enable;

    / * * * configuration swagger2 core configuration docket < / br > * swagger access path: http://localhost:8081/swagger-ui.html < br > * * * swagger business interface API@return* /
    @Bean
    public Docket restApiBusiness(a) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Service packet interface")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api_Business.class))
                .paths(PathSelectors.any())
                .build()
                .enable(enable);
    }

    @Bean
    public Docket restApiBase(a) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Basic interface")
                .apiInfo(apiInfoBase())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api_Base.class))
                .paths(PathSelectors.any())
                .build()
                .enable(enable);
    }

    private ApiInfo apiInfo(a) {
        return new ApiInfoBuilder()
                .title("Service packet interface")
                .contact(new Contact("ifknow"."http://www.gongshiyong.online"."[email protected]"))
                .description("Swagger ifknow test")
                .version("1.0.0")
                .termsOfServiceUrl("http://www,gongshiyong.online")
                .build();
    }

    private ApiInfo apiInfoBase(a) {
        return new ApiInfoBuilder()
                .title("Basic interface")
                .contact(new Contact("ifknow"."http://www.gongshiyong.online"."[email protected]"))
                .description("Swagger ifknow test")
                .version("1.0.0")
                .termsOfServiceUrl("http://www,gongshiyong.online") .build(); }}Copy the code
package com.ifknow.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/ * * *@Author: ifknow <br>
 * @Date: 2020/8/20  11:34 <br>
 * @Description: NO Description
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Api_Business {
}
Copy the code
package com.ifknow.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/ * * *@Author: ifknow <br>
 * @Date: 2020/8/20  11:50 <br>
 * @Description: NO Description
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Api_Base {
}
Copy the code

1.3 Write code tests

! [QQ screenshots20200820144932](C:\Users\hainei-developer\Pictures\Saved Pictures\blog\8.17- springboot \ QQ screenshots20200820144932.png)package com.ifknow.controller;

import com.ifknow.annotation.Api_Business;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/ * * *@Author: ifknow <br>
 * @Date: 2020/8/20  11:37 <br>
 * @Description: User information Controller */
@api (value = "user management - user information management Api ", tags =" user management - user information management Api ")
@RestController
@RequestMapping("/user")
@Api_Business
public class UserController {

    @apiOperation (value = "new user info ", notes =" new user info ")
    @PostMapping("/insert")
    public String insert(a) {
        return "success";
    }

    @apiOperation (value = "delete user information ", notes =" delete user information ")
    @DeleteMapping("/delete")
    public String delete(@RequestParam String id) {
        return "success";
    }

    @apiOperation (value = "modify user information ", notes =" modify user information ")
    @PutMapping("/update")
    public String update(a) {
        return "success";
    }

    @apiOperation (value = "query user information ", notes =" query user information ")
    @GetMapping("/pageInfo")
    public String pageInfo(a) {
        return "success"; }}Copy the code
  • Open a browser to http://localhost:8081/swagger-ui.html, you can see the methods defined in the controller, parameters and return values, etc.

2 Knife4j

2.1 Adding a Dependency

Knife4j’s predecessor was Swagger. Knife4j adds its own dependency to swagger’s dependency.

<! - integration Knife4j -- -- >
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <! -- Search for the latest version number in Maven's central repository when referencing -->
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>
Copy the code
  • Open a browser visit: http://localhost:8081/doc.html.

3 summary

I feel that there is no big difference between these two interfaces, as long as they are suitable.

Example code -GitHub

Example code -Gitee

Personal blog