增加参数精确校验,统一参数绑定异常,将异常通知处理类移入ssm基础项目

This commit is contained in:
lsy
2018-01-09 11:45:41 +08:00
parent 51cc5466be
commit 63f0a371fd
5 changed files with 36 additions and 22 deletions
@@ -10,7 +10,7 @@ import org.springframework.context.annotation.Configuration;
* @since 1.0
*/
@Configuration
@ComponentScan(basePackages = {"com.micro.fast.common.config"})
@ComponentScan(basePackages = {"com.micro.fast.common"})
public class CommonApplication {
// public static void main(String[] args) {
// SpringApplication.run(CommonApplication.class, args);
@@ -1,9 +1,10 @@
package com.micro.fast.upms.controller.handler;
package com.micro.fast.common.exception.handler;
import com.micro.fast.common.exception.SystemException;
import com.micro.fast.common.exception.UserException;
import com.micro.fast.util.ExceptionUtil;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -26,7 +27,7 @@ public class ExceptionHandlerController {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, String> handlerSystemException(SystemException se) {
String[] strings = ExceptionUtil.splitCodeMsg(se.getMessage());
Map<String,String> map = new HashMap<>();
Map<String,String> map = new HashMap<>(1);
map.put(strings[0],strings[1]);
return map;
}
@@ -40,7 +41,21 @@ public class ExceptionHandlerController {
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> handlerUserException(UserException se) {
String[] strings = ExceptionUtil.splitCodeMsg(se.getMessage());
Map<String,String> map = new HashMap<>();
Map<String,String> map = new HashMap<>(1);
map.put(strings[0],strings[1]);
return map;
}
/**
* 用于统一处理参数校验异常
* @param be
* @return
*/
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String,String> handlerBingException(BindException be){
String[] strings = ExceptionUtil.splitCodeMsg(be.getMessage());
Map<String,String> map = new HashMap<>(1);
map.put(strings[0],strings[1]);
return map;
}
@@ -1,5 +1,7 @@
package com.micro.fast.common.response;
import com.micro.fast.util.ExceptionUtil;
/**
* 数据字典
*
@@ -10,6 +12,9 @@ package com.micro.fast.common.response;
public class BaseConst {
public static final String DEFAULT_VALIDATE_CODE = "888888";
public static final String BASEMSG_PREFIX = DEFAULT_VALIDATE_CODE+ ExceptionUtil.split;
/**
* 服务器响应数据字典
*/
@@ -1,12 +0,0 @@
package com.micro.fast.common.util;
import com.micro.fast.util.ExceptionUtil;
/**
* 对于基础异常工具类进行封装
* @author lsy
*/
public class ExceptionHandlerUtil {
}
@@ -1,18 +1,24 @@
package com.micro.fast.upms.pojo;
import com.micro.fast.common.response.BaseConst;
import com.micro.fast.util.ExceptionUtil;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Email;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
public class UpmsUser implements Serializable {
private Integer id;
@NotBlank(message = BaseConst.DEFAULT_VALIDATE_CODE+ExceptionUtil.split+"请按提示输入用户名")
private Integer id;
/**
* 用户名必须为6-16位字母或数字的组合
*/
@Pattern(regexp = "^[a-z,A-Z,0-9]{6,16}$",message = BaseConst.BASEMSG_PREFIX+"请按要求输入用户名")
private String username;
@NotBlank(message = BaseConst.DEFAULT_VALIDATE_CODE+ExceptionUtil.split+"请按提示输入密码")
/**
* 密码必须为6-16位字母或数字的组合
*/
@Pattern(regexp = "^[a-z,A-Z,0-9]{6,16}$",message = BaseConst.BASEMSG_PREFIX+"请按要求输入密码")
private String password;
private String salt;
@@ -23,7 +29,7 @@ public class UpmsUser implements Serializable {
private String phone;
@NotBlank(message = BaseConst.DEFAULT_VALIDATE_CODE+ExceptionUtil.split+"请按照提示输入邮箱")
@Email(message = BaseConst.BASEMSG_PREFIX+"邮箱格式不正确")
private String email;
private Byte sex;