java 单机接口限流处理方案
594
2022-10-20
Spring4下validation数据校验无效(maven)的解决
SpringMVC4使用数据校验的时候需要使用hibernate-validator的包
第一步添加依赖
第二步添加校验注解
package com.demo.model;
/*
* spring4 用hibernate中的validation
*
* */
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.Size;
public class User {
private String username;
private String password;
private String nickname;
private String email;
public User(){
}
public User(String username, String password, String nickname, String email) {
this.username = username;
this.password =CdhNGfVVT password;
this.nickname = nickname;
this.email = email;
}
@NotBlank(message = "用户名不能为空")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Size(min = 6,max = 20,message = "密码为6-20位")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@Email(message = "请输入合法的邮箱")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
第三步在控制器中对指定参数进行校验注解
注:BindingResult 必须紧跟在@Validate
//@Validated指定验证对象 BindingResult紧跟Validated
@RequestMapping(value="/add",method = RequestMethod.POST)
public String add(@Validated User user, BindingResult br){
System.out.println(br);
System.out.println(br.getErrorCount());
if(br.hasErrors())
return "user/add";
users.put(user.getUsername(),user);
return "redirect:/user/users";
}
注:这个配置也会影响到校验是否生效
SpringBoot-----数据验证validation(实战中,不推荐使用)
1、定义错误信息
在resources/下创建名称为ValidationMessages.properties
member.mid.notnull.error=用户名不允许为空
member.mid.email.error=用户名的注册必须输入正确的邮箱
member.mid.length.error=用户名的格式错误
2、给Vo类添加注解
package com.zemel.micorboot.vo;
import java.io.Serializable;
import java.util.Date;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
public class Member implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@NotNull(message="{member.mid.notnull.error}")
@Email(message="{member.mid.email.error}")
@Length(min=6,message="{member.mid.length.error}")
private String mid;
private Integer age;
private Double salary;
private Date birthday;
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public DouCdhNGfVVTble getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3、控制添加验证
@Valid Member vo, BindingResult result
package com.zemel.micorboot.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zemel.micorboot.base.AbstractBaseController;
import com.zemel.micorboot.vo.Member;
@Controller
public class MemberController extends AbstractBaseController {
@GetMapping("/addPre")
public String addPre(){
return "message/member_add";
}
@GetMapping("/add")
@ResponseBody
public Object add(@Valid Member vo, BindingResult result){
System.out.println("【ADD方法】"+result.hasErrors());
if(result.hasErrors()){
List
list.forEach(oe -> {
});
return result.getAllErrors();
}
return vo;
}
}
4、访问结果
[{"codes":["Email.member.mid","Email.mid","Email.java.lang.String","Email"],"arguments":[{"codes":["member.mid","mid"],"arguments":null,"defaultMessage":"mid","code":"mid"},[],{"defaultMessage":".*","arguments":null,"codes":[".*"]}],"defaultMessage":"çæåçæ³åå¿¡»¾å¥æ£ç¡®ç®ç®","objectName":"member","field":"mid","rejectedValue":"xx","bindingFailure":false,"code":"Email"}]
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~