1. 程式人生 > >springboot bean上的屬性註解

springboot bean上的屬性註解

一、案例,前臺呼叫傳入name、age、phone、address、password然後後臺在插入資料庫之前進行校驗,判斷name、address、password是否為null或者empty,phone格式是否正確,年齡是否大於18歲等等

1、建立User

package com.cn.dl;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;

/**
 *  這三個註解的區別
 *  @NotEmpty 用在集合類上面;不能為null,而且長度必須大於0
 *  @NotBlank 用在String上面;只能作用在String上,不能為null,而且呼叫trim()後,長度必須大於0
 *  @NotNull 用在基本型別上;不能為null,但可以為empty。
 * Created by Tiger on 2018/10/23.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable{

    private static final long serialVersionUID = 8474075197757594232L;

    @NotBlank(message = "the name cannot be empty or null")
    private String name;
    @NotNull(message = "the age cannot be empty or null")
    @Min(value = 18,message = "the age must be greater than or equal to 18")
    private Integer age;
    @NotBlank(message = "the phone cannot be empty or null")
    @Pattern(regexp = "^[1][3,4,5,7,8][0-9]{9}$",message = "the phone number format error")
    private String phone;
    @NotBlank(message = "the address cannot be empty or null")
    private String address;
    @JsonIgnore
    @NotBlank(message = "the password cannot be empty or null")
    private String password;
}

2、建立Response

package com.cn.dl.response;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 請求返回類
 * Created by Tiger on 2018/10/9.
 */
@Getter
@Setter
//// TODO: 2018/10/27 @JsonSerialize 如果返回的json中有null值,忽略 
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
public class Response<T> implements Serializable {

    private static final long serialVersionUID = -4505655308965878999L;

    //請求成功返回碼為:0000
    public static final String successCode = "0000";
    //請求失敗返回碼為:9999
    public static final String failedCode = "9999";
    //返回資料
    private T data;
    //返回碼
    private String code;
    //返回描述
    private String msg;

    public Response(){
        this.code = successCode;
        this.msg = "請求成功";
    }

    public Response(String code, String msg){
        this();
        this.code = code;
        this.msg = msg;
    }
    public Response(String code, String msg, T data){
        this();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public Response(T data){
        this();
        this.data = data;
    }
}

3、建立UserController

package com.cn.dl.controller;

import com.cn.dl.User;
import com.cn.dl.response.Response;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by Tiger on 2018/10/26.
 */
@RestController
@RequestMapping("user")
public class UserController {

    @RequestMapping("insert")
    public Response insertUser(@Valid User user, BindingResult bindingResult){
        System.out.println(user.toString());
        if(bindingResult.hasErrors()){
            return new Response(Response.failedCode,bindingResult.getFieldError().getDefaultMessage());
        }
        //// TODO: 2018/10/27 儲存資料等等
        return new Response(user);
    }
}

@Valid:用於校驗User中的屬性,BindingResult:返回校驗失敗的資訊

二、測試

1、不輸入age、name、phone、address、password

2、輸入age小於18

3、輸入name為空串時

4、phone格式也是根據正則匹配、返回的User不包含password

5、如果去掉password上@JsonIgnore

{
    "data": {
        "name": "Tiger",
        "age": 18,
        "phone": "15769393859",
        "address": "beijing",
        "password": "123456"
    },
    "code": "0000",
    "msg": "請求成功"
}

三、分享一個postman小技巧:postman管理ip和port,這樣就不需要我們每次都去輸入ip和port了

  • 開啟Manage Environments

         

  • 編輯Environment,輸入Environment名字,輸入key和Value,然後儲存

   

  • Environment選擇剛才編輯的localhost,然後在輸入{ 就會看到剛才編輯的key,其值就是對應的value,然後我們輸入ip和port之外的url就ok了!