JSR303 data verification and multi-environment switching

JSR303 data verification

In Springboot, @validated can be used to verify data. If the data is abnormal, an exception will be thrown uniformly to facilitate the unified processing of the exception center. Let's write a note here so that our name can only support Email format;

@Component //注册bean
@ConfigurationProperties(prefix = "person")
@Validated  //数据校验
public class Person {
    
    

    @Email(message="邮箱格式错误") //name必须是邮箱格式
    private String name;
}

Operation result: default message [not a legal email address];
using data verification can ensure the correctness of the data;

Common parameters

@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;

空检查
@Null       验证对象是否为null
@NotNull    验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank   检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty   检查约束元素是否为NULL或者是EMPTY.
    
Booelan检查
@AssertTrue     验证 Boolean 对象是否为 true  
@AssertFalse    验证 Boolean 对象是否为 false  
    
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内  
@Length(min=, max=) string is between min and max included.

日期检查
@Past       验证 Date 和 Calendar 对象是否在当前时间之前  
@Future     验证 Date 和 Calendar 对象是否在当前时间之后  
@Pattern    验证 String 对象是否符合正则表达式的规则

...And so on.
In addition to this, we can also customize some data validation rules

Multi-environment switching

Profile is Spring's support for different configuration functions for different environments. It can switch environments quickly by activating different environment versions;

Multiple configuration files

When we write the main configuration file, the file name can be application-{profile}.properties/yml, which is used to specify multiple environment versions;

E.g:

application-test.properties represents the test environment configuration

application-dev.properties represents the development environment configuration

But Springboot does not directly start these configuration files, it uses the application.properties main configuration file by default;

We need to select the environment that needs to be activated through a configuration:

#比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试;
#我们启动SpringBoot,就可以看到已经切换到dev下的配置了;
spring.profiles.active=dev

yaml multi-document block

It is the same as in the properties configuration file, but using yml to achieve it does not need to create multiple configuration files, which is more convenient!

server:
  port: 8081
#选择要激活那个环境块
spring:
  profiles:
    active: prod

---
server:
  port: 8083
spring:
  profiles: dev #配置环境的名称


---

server:
  port: 8084
spring:
  profiles: prod  #配置环境的名称

Note: If both yml and properties are configured with ports and other environments are not activated, the properties configuration file will be used by default!

Configuration file loading location

There are many ways to load configuration files externally, we can choose the most commonly used and configure it in the development resource file!

Springboot startup will scan the application.properties or application.yml files in the following locations as the default configuration file for Spring boot:

优先级1:项目路径下的config文件夹配置文件
优先级2:项目路径下配置文件
优先级3:资源路径下的config文件夹配置文件
优先级4:资源路径下配置文件
优先级由高到底,高优先级的配置会覆盖低优先级的配置;

SpringBoot will load the main configuration file from these four locations; complementary configuration;

We set up a project access path configuration in the lowest level configuration file to test for complementary issues;

#配置项目的访问路径
server.servlet.context-path=/kuang

Expansion, operation and maintenance tips

Load configuration file at specified location

We can also change the default configuration file location through spring.config.location

After the project is packaged, we can use the form of command line parameters to specify the new location of the configuration file when starting the project; in this case, usually later operation and maintenance are done more, the same configuration, the externally specified configuration file has the highest priority

java -jar spring-boot-config.jar --spring.config.location=F:/application.properties

Guess you like

Origin blog.csdn.net/david2000999/article/details/114481474