关于项目中的一些常用配置类(持续更新)

一.yml配置

在开发实践中,我们通常都要配置开发环境配置文件,生产环境配置文件等,这些配置文件可以使用properties文件进行配置,也可以使用yml文件进行配置

yml类似于properties文件,是对全局的一些配置,比如数据库的连接,JSON格式默认包含的属性,spring中的一些个性化配置等

例如:

spring:
  # Profile配置
  profiles:
    # 激活Profile配置,配置开发环境dev
    active: dev
  # 连接数据库的相关配置
  datasource:
    # 使用的数据库连接池类型
    type: com.alibaba.druid.pool.DruidDataSource
  # 关于jackson的配置
  jackson:
    # 响应的JSON中默认包含哪些属性
    default-property-inclusion: non_null
# 配置编码格式
server:
  servlet:
    encoding:
     # 是否强制执行
      force: true
     # 编码格式
      charset: utf-8

# Mybatis相关配置
mybatis:
  # 用于配置SQL语句的XML文件的位置
  mapper-locations: classpath:mapper/*.xml

以上格式为yml文件的格式,之前properties文件中的配置,例如:spring.datasource.url,改写成yml之后就是上述的配置,spring之后输入冒号,接着回车,在spring下空两格(注意:下一级配置必须与上一级配置首行缩进两格),然后再输入datasource:再回车,接着输入url:,后面的jdbc部分必须在url后的冒号之后空一格输入,否则就会出现语法错误.

开发环境中的yml配置为:

# Spring系列框架的配置
spring:
  # 连接数据库的配置
  datasource:
    # 连接数据库的URL
    url: jdbc:mysql://localhost:3306/mall_ams?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    # 登录数据库的用户名
    username: root
    # 登录数据库的密码
    password: root

# 日志相关配置
logging:
  # 日志的显示级别
  level:
    # 根包的日志显示级别
    cn.tedu.boot.demo: trace

以上是一个springboot项目的基础配置,可以根据需要进行添加或删除

二.跨域配置

在前后端分离的开发实践中,前端页面应该与后端页面使用两个不同的地址运行,但是如果后端不进行跨域配置的话,那么前端发送的请求就会被服务端拒绝,所以我们要先在后端进行跨域的配置

具体如下:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

}

需要注意的是,该配置类必须实现WebMvcConfigurer接口,并且重写addCorsMappings方法,该配置类的写法相对比较固定.

三.Knife4j配置

Knife4j是一款在线API文档的框架,是基于Swagger框架实现的,要配置该框架需要三步:

1.添加依赖项,可以从官网上找到对应的项目的依赖项,此处写的是基于springboot项目的依赖项

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.9</version>
</dependency>

2.添加配置类

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
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.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    /**
     * 【重要】指定Controller包路径
     */
    private String basePackage = "指定控制器包路径,必填";
    /**
     * 分组名称
     */
    private String groupName = "分组名称";
    /**
     * 主机名
     */
    private String host = "主机名";
    /**
     * 标题
     */
    private String title = "标题";
    /**
     * 简介
     */
    private String description = "简介";
    /**
     * 服务条款URL
     */
    private String termsOfServiceUrl = "http://www.apache.org/licenses/LICENSE-2.0";
    /**
     * 联系人
     */
    private String contactName = "联系人";
    /**
     * 联系网址
     */
    private String contactUrl = "联系网址";
    /**
     * 联系邮箱
     */
    private String contactEmail = "联系邮箱";
    /**
     * 版本号
     */
    private String version = "1.0.0";

    @Autowired
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    public Docket docket() {
        String groupName = "1.0.0";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .host(host)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName));
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }
}

以上配置类中,除了控制器包路径必须要指明,其余的类似邮箱之类的可以自行定义或者不定义,另外,本配置类写法固定,直接引用即可.

PS:

@Autowired
    private OpenApiExtensionResolver openApiExtensionResolver;

此处会报错,因为还有一步没有配置,配置完第三步,这边的报错就会解决

3.在yml或properties文件中开启,此处是在yml文件中的配置

knife4j:
  enable: true

完成了以上三步之后,就可以成功使用Knife4j在线API文档啦,该文档是基于Swagger开发的,因此Swagger中能使用的注解,在该框架中也可以使用,两者使用基本一致.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值