Jboot v3.7.5 released, the most elegant data verification method on the entire network

Jboot is a microservice framework developed based on JFinal, JFinal-Undertow, Dubbo, Seata, Sentinel, ShardingSphere, Nacos, etc., to help developers lower the threshold for microservice development. At the same time, it perfectly supports multiple maven modules under idea and eclipse, and hot-loading resource files such as java code, html, css, js, etc. Shuangshuang development, happy life.

So far, Jboot has been open source for more than 4 years, iterated 160+ versions, and has been used by more than 1,000 companies, including many well-known listed companies.

Jboot V3.7.5 enhances the verification method of Jboot. Based on the @EmptyValidate and @RegexValidate before Jboot, the verification method is further simplified based on JSR 303-Bean Validation, which is more elegant and simple than Spring.

@NotNull verification

In the Controller (or Service, etc.), we can directly add to the Controller via the @NotNull annotation, for example:

 public void test(@NotNull String para) {
        renderText("test6");
    }

You may say that adding @NotNull to the parameter is also supported in Spring, but if you use @Size and other verification, Spring cannot directly add the parameter, such as:

 public void test(@Size(min=2,max=10) String para) {
        renderText("test6");
    }

This requires that the length of the para parameter passed by the front end must be between 2 and 10 directly.

@Size verification

@Size verification can not only verify the length of String data, but also verify the size range of values ​​of data types such as int long. such as:

 public void test(@Size(min=2,max=10) int value) {
        renderText("test6");
    }

This requires that the value of value must be between 2 and 10 directly.

Of course, we can also use @Size to verify the length of the Map/List/array, such as with @JsonBody to receive the value passed in from the front end:

public void list(@Size(min=2,max=10) @JsonBody() List<MyBean> list) {        
      System.out.println("list--->" + list);        
      renderText("ok");
  }

It is required that the length of the MyBean Json array passed in earlier must be between 2 and 10.

@NotEmpty verification

@NotEmpty can not only verify that the String type cannot be null or empty string, but also verify that Map, List, array, etc. cannot be empty, such as:

public void list(@NotEmpty() @JsonBody() List<MyBean> list) {        
      System.out.println("list--->" + list);        
      renderText("ok");
  }

The MyBean Json array incorporated by the front end must have a value.

@Valid validation

@Valid is for the entire Java Bean validation, and it can also validate the JFinal Model. Number of MyBean Json

For example, MyBean is defined as follows:

public class MyBean {
    private String id;  
 
    @NotBlank(message = "密码不能为空")
    private String password;

    @Size(min=0,max=2,message = "性别的值只能是 0 1 2")
    private int sex;

    @Min(value = 18,message = "未成年禁止入内")  
    private Integer age; 
}

 In Controller or Service, the following code can directly verify MyBean:

 public void test(@Valid() MyBean bean) {
        renderText("test6");
    }

If MyBean is a JFinal Model, we only need to add annotations to the getter method.

In addition to the basic examples above, Jboot's verification also supports more verification:

annotation Description
@NotNull Limit must not be null
@DecimalMax(value) The limit must be a number not greater than the specified value
@DecimalMin(value) The limit must be a number not less than the specified value
@Digits(integer,fraction) The limit must be a decimal, and the digits of the integer part cannot exceed integer, and the digits of the decimal part cannot exceed fraction
@Max(value) The limit must be a number not greater than the specified value
@Min(value) The limit must be a number not less than the specified value
@Pattern(value) The restriction must conform to the specified regular expression
@Size(max,min) Limit the character length must be between min and max
@NotEmpty Verify that the element value of the annotation is not null and not empty (string length is not 0, collection size is not 0)
@NotBlank Verify that the element value of the annotation is not empty (not null, and the length is 0 after removing the first space), which is different from @NotEmpty, @NotBlank is only applied to the string and will remove the space of the string during comparison
@Email Verify that the element value of the annotation is Email, and you can also specify a custom email format through regular expressions and flags

In the use of writing verification annotations, we not only verify the http request data in the Controller, but also verify the incoming parameters in the Service (provider).

In the front-end, if the verification fails, the following Json content will be returned (you can modify the following content by configuring ErrorRender):

{
    "throwable": "io.jboot.components.valid.ValidException: 必须小于或等于200",
    "errorMessage": "age max value is 200, but current value is 1001 at method: io.jboot.test.validate.ValidateController.test17(int)",
    "errorCode": 400,
    "state": "fail",
    "message": "必须小于或等于200"
}

Validation failure in the Service will throw a ValidException exception.

Of course, there are more functions here. I will not give examples one by one here. Welcome to let your friends get cool when they use Jboot to develop.

Development documents:

https://jbootprojects.gitee.io/docs/

At the same time, Jboot officially launched the enterprise-level development framework JbootAdmin, details  https://jbootprojects.gitee.io/docs/jbootadmin/

maven dependency:

<dependency>
    <groupId>io.jboot</groupId>
    <artifactId>jboot</artifactId>
    <version>3.7.5</version>
</dependency>

Hello World:

@RequestMapping("/")
public class HelloworldController extends JbootController {

    public void index(){
        renderText("hello world");
    }

    public static void main(String[] args){
        JbootApplication.run(args);
    }
}

Guess you like

Origin www.oschina.net/news/124287/jboot-3-7-5-released