添加多运行环境支持 #14

Open
xiaohai2271 wants to merge 36 commits from feat-multlyEnv#13 into master-old
4 changed files with 37 additions and 16 deletions
Showing only changes of commit 91de56cb32 - Show all commits

View File

@@ -11,22 +11,17 @@ import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.ConfigMapper; import cn.celess.blog.mapper.ConfigMapper;
import cn.celess.blog.mapper.UserMapper; import cn.celess.blog.mapper.UserMapper;
import cn.celess.blog.service.InstallService; import cn.celess.blog.service.InstallService;
import cn.celess.blog.service.fileserviceimpl.LocalFileServiceImpl;
import cn.celess.blog.util.MD5Util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import java.io.*; import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
/** /**
* @author : xiaohai * @author : xiaohai
@@ -35,6 +30,7 @@ import java.util.stream.Collectors;
*/ */
@Slf4j @Slf4j
@Controller @Controller
@Validated
public class InstallController { public class InstallController {
@Autowired @Autowired
@@ -53,9 +49,8 @@ public class InstallController {
@PostMapping("/install") @PostMapping("/install")
@ResponseBody @ResponseBody
@Transactional(rollbackFor = Exception.class) public Response<Object> install(@Valid @RequestBody InstallParam installParam) {
public Response install(@RequestBody InstallParam installParam) { return Response.success(installService.install(installParam));
return Response.success(installParam);
} }

View File

@@ -2,6 +2,8 @@ package cn.celess.blog.entity;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank;
/** /**
* @author : xiaohai * @author : xiaohai
* @date : 2020/10/18 14:52 * @date : 2020/10/18 14:52
@@ -9,14 +11,25 @@ import lombok.Data;
*/ */
@Data @Data
public class InstallParam { public class InstallParam {
@NotBlank(message = "数据库类型不可为空")
private String dbType; private String dbType;
@NotBlank(message = "数据库链接不可为空")
private String dbUrl; private String dbUrl;
@NotBlank(message = "数据库用户名不可为空")
private String dbUsername; private String dbUsername;
@NotBlank(message = "数据库密码不可为空")
private String dbPassword; private String dbPassword;
/** /**
* user 相关 * user 相关
*/ */
@NotBlank(message = "用户邮箱地址不可为空")
private String email; private String email;
@NotBlank(message = "用户密码不可为空")
private String password; private String password;
} }

View File

@@ -10,7 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.SimpleMailMessage;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -18,6 +20,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;
/** /**
* @author : xiaohai * @author : xiaohai
@@ -41,27 +47,32 @@ public class ExceptionHandle {
if (e instanceof MyException) { if (e instanceof MyException) {
MyException exception = (MyException) e; MyException exception = (MyException) e;
logger.debug("返回了自定义的exception,[code={},msg={},result={}]", exception.getCode(), e.getMessage(), exception.getResult()); logger.debug("返回了自定义的exception,[code={},msg={},result={}]", exception.getCode(), e.getMessage(), exception.getResult());
return new Response(exception.getCode(), e.getMessage(), exception.getResult()); return new Response<>(exception.getCode(), e.getMessage(), exception.getResult());
} }
//请求路径不支持该方法 //请求路径不支持该方法
if (e instanceof HttpRequestMethodNotSupportedException) { if (e instanceof HttpRequestMethodNotSupportedException) {
logger.debug("遇到请求路径与请求方法不匹配的请求,[msg={}path:{},method:{}]", e.getMessage(), request.getRequestURL(), request.getMethod()); logger.debug("遇到请求路径与请求方法不匹配的请求,[msg={}path:{},method:{}]", e.getMessage(), request.getRequestURL(), request.getMethod());
return new Response(ResponseEnum.ERROR.getCode(), e.getMessage(), null); return new Response<>(ResponseEnum.ERROR.getCode(), e.getMessage(), null);
} }
//数据输入类型不匹配 //数据输入类型不匹配
if (e instanceof MethodArgumentTypeMismatchException) { if (e instanceof MethodArgumentTypeMismatchException) {
logger.debug("输入类型不匹配,[msg={}]", e.getMessage()); logger.debug("输入类型不匹配,[msg={}]", e.getMessage());
return new Response(ResponseEnum.PARAMETERS_ERROR.getCode(), "数据输入有问题,请修改后再访问", null); return new Response<>(ResponseEnum.PARAMETERS_ERROR.getCode(), "数据输入有问题,请修改后再访问", null);
} }
//数据验证失败 //数据验证失败
if (e instanceof BindException) { if (e instanceof BindException) {
logger.debug("数据验证失败,[msg={}]", e.getMessage()); logger.debug("数据验证失败,[msg={}]", e.getMessage());
return new Response(ResponseEnum.PARAMETERS_ERROR.getCode(), "数据输入有问题,请修改", null); return new Response<>(ResponseEnum.PARAMETERS_ERROR.getCode(), "数据输入有问题,请修改", null);
} }
//数据输入不完整 //数据输入不完整
if (e instanceof MissingServletRequestParameterException) { if (e instanceof MissingServletRequestParameterException) {
logger.debug("数据输入不完整,[msg={}]", e.getMessage()); logger.debug("数据输入不完整,[msg={}]", e.getMessage());
return new Response(ResponseEnum.PARAMETERS_ERROR.getCode(), "数据输入不完整,请检查", null); return new Response<>(ResponseEnum.PARAMETERS_ERROR.getCode(), "数据输入不完整,请检查", null);
}
if (e instanceof MethodArgumentNotValidException) {
logger.debug("数据验证失败,[msg: {}]", e.getMessage());
BindingResult violations = ((MethodArgumentNotValidException) e).getBindingResult();
return new Response<>(ResponseEnum.PARAMETERS_ERROR.getCode(), violations.getAllErrors().get(0).getDefaultMessage(), null);
} }
// 发送错误信息到邮箱 // 发送错误信息到邮箱
@@ -70,7 +81,7 @@ public class ExceptionHandle {
sendMessage(e); sendMessage(e);
} }
e.printStackTrace(); e.printStackTrace();
return new Response(ResponseEnum.ERROR.getCode(), "服务器出现错误,已记录", null); return new Response<>(ResponseEnum.ERROR.getCode(), "服务器出现错误,已记录", null);
} }
/** /**

View File

@@ -16,6 +16,7 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.File; import java.io.File;
@@ -62,6 +63,7 @@ public class InstallServiceImpl implements InstallService {
@SneakyThrows @SneakyThrows
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> install(@NotNull InstallParam installParam) { public Map<String, Object> install(@NotNull InstallParam installParam) {
User user = new User(installParam.getEmail(), MD5Util.getMD5(installParam.getPassword())); User user = new User(installParam.getEmail(), MD5Util.getMD5(installParam.getPassword()));
userMapper.addUser(user); userMapper.addUser(user);