依赖:
<!--引入knife4j依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
knife4j配置文件:
package com.example.family.Configuration;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Configuration
@EnableSwagger2//Swagger3版本使用这个注解
public class Knife4jConfiguration {
/*引入Knife4j提供的扩展类*/
private final OpenApiExtensionResolver openApiExtensionResolver;
@Autowired
public Knife4jConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
this.openApiExtensionResolver = openApiExtensionResolver;
}
@Bean(value = "defaultApi2")
public Docket userApi() {
String groupName = "用户管理API";
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName(groupName)
.select()
//在设置的包下的会生效
.apis(RequestHandlerSelectors.basePackage("com.example.family.Controller.UserController"))
//只有标注了ApiOperation的方法才会生效
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
//赋予插件体系
.extensions(openApiExtensionResolver.buildExtensions(groupName));
}
@Bean(value = "defaultApi3")
public Docket testApi() {
String groupName = "测试API";
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName(groupName)
.select()
//在设置的包下的会生效
.apis(RequestHandlerSelectors.basePackage("com.example.family.Controller.Test"))
//只有标注了ApiOperation的方法才会生效
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
//赋予插件体系
.extensions(openApiExtensionResolver.buildExtensions(groupName));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("family后台服务API")
.description("整合knife4j搭建的后台服务API接口文档")
.contact(new Contact("减肥", null, "[email protected]"))
.termsOfServiceUrl("http://localhost:8080/")
.version("1.0.0")
.build();
}
}
controller层:
package com.example.family.Controller.UserController;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.example.family.DAO.UserDAO;
import com.example.family.DTO.UserQueryDTo;
import com.example.family.Pojo.User;
import com.example.family.ResponseDTO.Result;
import com.example.family.ResponseDTO.ResultEntry;
import com.example.family.ResponseDTO.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Api(tags = "用户管理")
@RequestMapping("user")
public class UserController {
@Autowired
UserDAO userDAO;
@ApiOperation("查询用户")
@GetMapping("search")
public Result<List<User>> test1(UserQueryDTo dto){
User user = new User();
BeanUtil.copyProperties(dto,user, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
Example<User> example = Example.of(user);
return ResultEntry.error(userDAO.findAll(example), ResultEnum.RESULT_NOT_EXIST);
}
}
报错信息:Unable to scan documentation context default java.lang.NullPointerException: null
出错原因:将类中的属性设置为了public类型
解决办法:将public删除或者改为private就可以啦
标签:knife4j,springBoot,family,documentation,groupName,报错,import,com,example From: https://www.cnblogs.com/cgdbk/p/17226970.html