处理异常处理逻辑
This commit is contained in:
+5
-2
@@ -1,12 +1,15 @@
|
||||
package com.micro.fast.boot.starter.common.exception;
|
||||
|
||||
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
import com.micro.fast.boot.starter.common.util.ExceptionUtil;
|
||||
|
||||
/**
|
||||
* 系统级别的异常,比如数据哭插入失败
|
||||
* @author lsy
|
||||
*/
|
||||
public class SystemException extends RuntimeException {
|
||||
public SystemException(String message) {
|
||||
super(message);
|
||||
public SystemException(BaseConst.SystemResponse systemResponse) {
|
||||
super(ExceptionUtil.contactCodeMsg(systemResponse.getCode(),systemResponse.getMsg()));
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -1,11 +1,15 @@
|
||||
package com.micro.fast.boot.starter.common.exception;
|
||||
|
||||
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
import com.micro.fast.boot.starter.common.util.ExceptionUtil;
|
||||
|
||||
/**
|
||||
* 用户异常类
|
||||
* @author lsy
|
||||
*/
|
||||
public class UserException extends RuntimeException{
|
||||
public UserException(String message) {
|
||||
super(message);
|
||||
public UserException(BaseConst.UserResponse userResponse) {
|
||||
super(ExceptionUtil.contactCodeMsg(userResponse.getCode(),userResponse.getMsg()));
|
||||
}
|
||||
}
|
||||
|
||||
+11
-9
@@ -1,13 +1,16 @@
|
||||
package com.micro.fast.boot.starter.common.exception.handler;
|
||||
|
||||
import com.micro.fast.boot.starter.common.exception.SystemException;
|
||||
import com.micro.fast.boot.starter.common.response.ServerResponse;
|
||||
import com.micro.fast.boot.starter.common.util.ExceptionUtil;
|
||||
import com.micro.fast.boot.starter.common.exception.UserException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -23,11 +26,11 @@ public class BaseExceptionHandlerController {
|
||||
*/
|
||||
@ExceptionHandler(SystemException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Map<String, String> handlerSystemException(SystemException se) {
|
||||
public ServerResponse handlerSystemException(SystemException se) {
|
||||
String[] strings = ExceptionUtil.splitCodeMsg(se.getMessage());
|
||||
Map<String,String> map = new HashMap<>(1);
|
||||
map.put(strings[0],strings[1]);
|
||||
return map;
|
||||
return ServerResponse.errorMsgData("系统异常",map);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,11 +40,11 @@ public class BaseExceptionHandlerController {
|
||||
*/
|
||||
@ExceptionHandler(UserException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Map<String, String> handlerUserException(UserException se) {
|
||||
public ServerResponse handlerUserException(UserException se) {
|
||||
String[] strings = ExceptionUtil.splitCodeMsg(se.getMessage());
|
||||
Map<String,String> map = new HashMap<>(1);
|
||||
map.put(strings[0],strings[1]);
|
||||
return map;
|
||||
return ServerResponse.errorMsgData("处理用户异常",map);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,11 +54,10 @@ public class BaseExceptionHandlerController {
|
||||
*/
|
||||
@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;
|
||||
public ServerResponse handlerBingException(BindException be){
|
||||
BindingResult bindingResult = be.getBindingResult();
|
||||
Map<String, String> validateExceptionInfo = ExceptionUtil.getValidateExceptionInfo(bindingResult);
|
||||
return ServerResponse.errorMsgData("参数校验失败",validateExceptionInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-1
@@ -10,7 +10,9 @@ import com.micro.fast.boot.starter.common.util.ExceptionUtil;
|
||||
* @since 1.0
|
||||
*/
|
||||
public class BaseConst {
|
||||
|
||||
/**
|
||||
* 参数校验发出的异常
|
||||
*/
|
||||
public static final String DEFAULT_VALIDATE_CODE = "888888";
|
||||
|
||||
public static final String BASEMSG_PREFIX = DEFAULT_VALIDATE_CODE+ ExceptionUtil.split;
|
||||
|
||||
+3
@@ -129,4 +129,7 @@ public class ServerResponse<T> {
|
||||
public static <T> ServerResponse<T> errorCodeMsg(Integer code, String msg) {
|
||||
return new ServerResponse<T>(code, msg);
|
||||
}
|
||||
public static <T> ServerResponse<T> errorMsgData(String msg,T data){
|
||||
return new ServerResponse<>(BaseConst.ServerResponseCode.ERROR.getCode(),msg,data);
|
||||
}
|
||||
}
|
||||
|
||||
+24
@@ -1,5 +1,11 @@
|
||||
package com.micro.fast.boot.starter.common.util;
|
||||
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 对异常处理的工具类
|
||||
* @author lsy
|
||||
@@ -28,4 +34,22 @@ public class ExceptionUtil {
|
||||
String[] split = codeMsg.split(ExceptionUtil.split);
|
||||
return split;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数绑定异常进行处理
|
||||
*/
|
||||
public static Map<String,String> getValidateExceptionInfo(BindingResult errors){
|
||||
Map<String,String> map = new HashMap<>(5);
|
||||
if (errors.hasErrors()) {
|
||||
errors.getAllErrors().stream().forEach(objectError -> {
|
||||
FieldError fieldError = (FieldError) objectError;
|
||||
// 接收参数绑定异常信息
|
||||
String defaultMessage = fieldError.getDefaultMessage();
|
||||
// 对信息进行拆分放入map
|
||||
String[] strings = splitCodeMsg(defaultMessage);
|
||||
map.put(strings[0],strings[1]);
|
||||
});
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.micro.fast.upms.controller;
|
||||
|
||||
import com.micro.fast.boot.starter.common.exception.SystemException;
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
import com.micro.fast.boot.starter.common.response.ServerResponse;
|
||||
import com.micro.fast.upms.dao.UpmsSystemMapper;
|
||||
import com.micro.fast.upms.pojo.UpmsSystem;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* 系统管理
|
||||
* @author lsy
|
||||
*/
|
||||
@Api("系统管理")
|
||||
@RestController
|
||||
@RequestMapping("/system")
|
||||
public class UpmsSystemController {
|
||||
|
||||
@Autowired
|
||||
private UpmsSystemMapper upmsSystemMapper;
|
||||
|
||||
@ApiOperation("添加系统")
|
||||
@PostMapping
|
||||
public ServerResponse addSystem(@Valid UpmsSystem upmsSystem,BindingResult errors) throws BindException {
|
||||
upmsSystem.setCtime(System.currentTimeMillis());
|
||||
upmsSystem.setStatus((byte)1);
|
||||
//对参数的校验抛出异常
|
||||
int i = upmsSystemMapper.insertSelective(upmsSystem);
|
||||
if (i!=1){
|
||||
throw new SystemException(BaseConst.SystemResponse.DATABASE_INSERT_FAILURE);
|
||||
}
|
||||
return ServerResponse.successMsg("添加系统成功");
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,32 @@
|
||||
package com.micro.fast.upms.controller;
|
||||
|
||||
import com.micro.fast.boot.starter.common.exception.UserException;
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
import com.micro.fast.boot.starter.common.response.ServerResponse;
|
||||
import com.micro.fast.boot.starter.common.util.ExceptionUtil;
|
||||
import com.micro.fast.upms.dao.UpmsUserMapper;
|
||||
import com.micro.fast.upms.pojo.UpmsUser;
|
||||
import com.micro.fast.upms.service.UpmsUserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 权限管理系统用户管理
|
||||
*
|
||||
* @author lsy
|
||||
*/
|
||||
@Api(description = "后台用户api")
|
||||
@RestController
|
||||
@RequestMapping(value = "/upms/user")
|
||||
@Api(description = "后台用户api")
|
||||
public class UpmsUserController {
|
||||
|
||||
/**
|
||||
@@ -38,24 +43,17 @@ public class UpmsUserController {
|
||||
* BindingResult一定要紧跟校验的对象
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation(value = "后台用户注册")
|
||||
public ServerResponse<UpmsUser> register(@Valid UpmsUser upmsUser,BindingResult errors
|
||||
, @RequestParam(defaultValue = "") String repeatPassword) {
|
||||
//对参数的校验抛出异常
|
||||
if (errors.hasErrors()) {
|
||||
errors.getAllErrors().stream().forEach(objectError -> {
|
||||
FieldError fieldError = (FieldError) objectError;
|
||||
throw new UserException(fieldError.getDefaultMessage());
|
||||
});
|
||||
}
|
||||
@PostMapping
|
||||
public ServerResponse<UpmsUser> register(@Valid UpmsUser upmsUser,@RequestParam(defaultValue = "") String repeatPassword) {
|
||||
ServerResponse<UpmsUser> register = upmsUserService.register(upmsUser, repeatPassword);
|
||||
return register;
|
||||
}
|
||||
|
||||
@GetMapping("/{username}")
|
||||
@ApiOperation(value = "根据用户名称获取用户详细信息")
|
||||
public ServerResponse getUserByName(@PathVariable(name = "username") String username){
|
||||
@GetMapping("/{username}")
|
||||
public ServerResponse getUserByName(
|
||||
@PathVariable(name = "username") @NotNull(message = ExceptionUtil.split+"用户名不能为空") String username){
|
||||
UpmsUser upmsUser = upmsUserMapper.selectByUsername(username);
|
||||
if (upmsUser ==null){
|
||||
return ServerResponse.errorMsg("该用户不存在!");
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.micro.fast.upms.pojo;
|
||||
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class UpmsSystem implements Serializable {
|
||||
@@ -11,14 +14,17 @@ public class UpmsSystem implements Serializable {
|
||||
|
||||
private String theme;
|
||||
|
||||
@NotNull(message = BaseConst.BASEMSG_PREFIX+"请填写系统路径")
|
||||
private String basepath;
|
||||
|
||||
private Byte status;
|
||||
|
||||
@NotNull(message = BaseConst.BASEMSG_PREFIX+"请填写系统名称")
|
||||
private String name;
|
||||
|
||||
private String title;
|
||||
|
||||
@NotNull(message = BaseConst.BASEMSG_PREFIX+"请填写系统描述")
|
||||
private String description;
|
||||
|
||||
private Long ctime;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.micro.fast.upms.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
@@ -8,7 +7,6 @@ import org.hibernate.validator.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import java.io.Serializable;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
public class UpmsUser implements Serializable {
|
||||
|
||||
private Integer id;
|
||||
|
||||
@@ -32,25 +32,18 @@ public class UpmsUserServiceImpl implements UpmsUserService {
|
||||
ServerResponse checkEmail = checkEmail(upmsUser.getEmail());
|
||||
//校验两次输入密码是否一致
|
||||
if (!upmsUser.getPassword().equals(repeatPassword)){
|
||||
throw new UserException(ExceptionUtil.contactCodeMsg(
|
||||
UserResponse.TWO_PASSWORD_NOT_SAME.getCode(),
|
||||
UserResponse.TWO_PASSWORD_NOT_SAME.getMsg()
|
||||
));
|
||||
throw new UserException(UserResponse.TWO_PASSWORD_NOT_SAME);
|
||||
}
|
||||
//校验用户名,邮箱是否存在
|
||||
boolean success = checkUsername.isSuccess();
|
||||
boolean success1 = checkEmail.isSuccess();
|
||||
if (!success){
|
||||
throw new UserException(ExceptionUtil.contactCodeMsg(
|
||||
UserResponse.USERNAME_IS_HAVE.getCode()
|
||||
,UserResponse.USERNAME_IS_HAVE.getMsg()));
|
||||
throw new UserException(UserResponse.USERNAME_IS_HAVE );
|
||||
}
|
||||
if (!success1){
|
||||
throw new UserException(ExceptionUtil.contactCodeMsg(
|
||||
UserResponse.EMAIL_IS_HAVE.getCode()
|
||||
,UserResponse.EMAIL_IS_HAVE.getMsg()));
|
||||
throw new UserException(UserResponse.EMAIL_IS_HAVE);
|
||||
}
|
||||
upmsUser.setCtime(Long.valueOf(System.currentTimeMillis()));
|
||||
upmsUser.setCtime(System.currentTimeMillis());
|
||||
String encode = passwordEncoder.encode(upmsUser.getPassword());
|
||||
upmsUser.setPassword(encode);
|
||||
byte loacked = 0;
|
||||
@@ -58,9 +51,7 @@ public class UpmsUserServiceImpl implements UpmsUserService {
|
||||
int i = upmsUserMapper.insertSelective(upmsUser);
|
||||
//是否插入成功
|
||||
if (i<1){
|
||||
throw new SystemException(ExceptionUtil.contactCodeMsg(
|
||||
SystemResponse.DATABASE_INSERT_FAILURE.getCode()
|
||||
,SystemResponse.DATABASE_INSERT_FAILURE.getMsg()));
|
||||
throw new SystemException(SystemResponse.DATABASE_INSERT_FAILURE);
|
||||
}
|
||||
return ServerResponse.successData(upmsUser);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#mybatisGeneratorExtend\u914D\u7F6E\u6587\u4EF6
|
||||
#mybatisGeneratorExtendÅäÖÃÎļþ
|
||||
db.driverLocation=/root/mysql-connector-java-5.1.40-bin.jar
|
||||
db.driverClassName=com.mysql.jdbc.Driver
|
||||
db.url=jdbc:mysql://127.0.0.1:3317/micro?useUncoide=true&chracterEncoding=utf-8&useSSL=false
|
||||
|
||||
Reference in New Issue
Block a user