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了!