PS:本来用的是2.x的,但是因为换了后端框架,导致版本不适配,于是调整到3.0
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>
由于由hibernate切换到了mybatisplus,导致主键的生成方式变化,使用Mybatisplus的代码生成器,自动生成的ID是BigDecimal
@TableId("ID")private BigDecimal id;
在使用Swagger接口文档中(http://xxx/swagger-ui/index.html)测试过程中发现BigDecimal 精度丢失,于是开始进行类型兼容
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;import java.math.BigDecimal;@Configuration
public class JacksonConfig {@Beanpublic ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {ObjectMapper objectMapper = builder.createXmlMapper(false).build();// 全局配置序列化返回 JSON 处理SimpleModule simpleModule = new SimpleModule();// 将使用String来序列化BigDecimal类型simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);objectMapper.registerModule(simpleModule);return objectMapper;}
}
import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
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.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;@Configuration
@EnableOpenApi
public class SwaggerConfig {@Autowiredprivate TypeResolver typeResolver;/*** 创建API应用* apiInfo() 增加API相关信息* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,* 本例采用指定扫描的包路径来定义指定要建立API的目录。** @return*///再定义一个Docket
@Beanpublic Docket desertsApi2() {return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo("Swagger3.0", "1.0")).select().apis(RequestHandlerSelectors.basePackage("com.xxx.controller")).paths(PathSelectors.any()).build().directModelSubstitute(BigDecimal.class, String.class).additionalModels(typeResolver.resolve(XXX.class), typeResolver.resolve(XXXX.class)) //这里将'YourEntity'类加入swagger文档中,如果实体类没有在接口中作为入参,那么不会直接在Schemas中显示,所以这里可以手动列出一些Eentity类.groupName("xxxx").enable(true);}/*** 创建该API的基本信息(这些基本信息会展现在文档页面中)* 访问地址:http://ip:port/swagger-ui.html** @return*/private ApiInfo apiInfo(String title, String version) {return new ApiInfoBuilder().title(title).description("api信息").contact(new Contact("GMS", "", "")).termsOfServiceUrl("").version(version).build();}@Beanpublic static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {return new BeanPostProcessor() {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {customizeSpringfoxHandlerMappings(getHandlerMappings(bean));}return bean;}private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());mappings.clear();mappings.addAll(copy);}@SuppressWarnings("unchecked")private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {try {Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");field.setAccessible(true);return (List<RequestMappingInfoHandlerMapping>) field.get(bean);} catch (IllegalArgumentException | IllegalAccessException e) {throw new IllegalStateException(e);}}};}
}
Mybatisplus 分页插件
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;import java.util.Collections;@EnableTransactionManagement
@Configuration
@MapperScan("com.xxx.mapper")
public class MybatisPlusConfig {/*** 分页插件 3.5.X* @author zhengkai.blog.csdn.net*/@Beanpublic PaginationInnerInterceptor paginationInnerInterceptor() {PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();// 设置最大单页限制数量,默认 500 条,-1 不受限制paginationInterceptor.setMaxLimit(-1L);paginationInterceptor.setDbType(DbType.ORACLE);// 开启 count 的 join 优化,只针对部分 left joinpaginationInterceptor.setOptimizeJoin(true);return paginationInterceptor;}@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();mybatisPlusInterceptor.setInterceptors(Collections.singletonList(paginationInnerInterceptor()));return mybatisPlusInterceptor;}
}
public ApiResult page(XXXDTO xxxDTO, int current, int size) {IPage<CBRMLyt> page = new Page<>(current,size);QueryWrapper<CBRMLyt> queryWrapper = new QueryWrapper<>();if (null != xxxDTO.getXXX() && xxxDTO.getXXX().length > 0) {queryWrapper.in("XX", Arrays.asList(xxxDTO.getXXX()));}queryWrapper.orderByAsc("XX1", "XX2");return ApiResult.ok(lytMapper.selectPage(page, queryWrapper));}