版权声明:分享知识是一种快乐,愿你我都能共同成长! https://blog.csdn.net/qidasheng2012/article/details/84548833
一、业务层结构图
ProjectModule 项目模块
package com.demo.saas.basic;
/**
* service模块
*/
public interface ProjectModule {
/**
* 基础服务
*/
public final static String BASIC = "basic";
/**
* 订单服务
*/
public final static String ORDER = "order";
}
serviceException.properties
49001=请求参数为空
49002=请求boss失败,请与管理员联系
49003=请求boss失败,http返回码:【{0}】,请与管理员联系
49004=请求失败:【{0}】,请与管理员联系
49005=请求boss返回数据异常
49006=请求参数错误,开始时间【{0}】晚于结束时间【{1}】
BasicException
package com.demo.saas.basic;
public class BasicException extends RuntimeException {
private int reason = 0;
private String[] params = null;
private static final long serialVersionUID = -3643557830604406276L;
public BasicException() {
super();
}
public BasicException(int reason) {
this.reason = reason;
}
public BasicException(String message) {
super(message);
}
public BasicException(Throwable cause) {
super(cause);
}
public BasicException(int reason, String[] params) {
this.reason = reason;
this.params = params;
}
public BasicException(int reason, Throwable cause) {
super(cause);
this.reason = reason;
}
public BasicException(String message, Throwable cause) {
super(message, cause);
}
public BasicException(int reason, String[] params, Throwable cause) {
super(cause);
this.reason = reason;
this.params = params;
}
public int getReason() {
return reason;
}
public void setReason(int reason) {
this.reason = reason;
}
public String[] getParams() {
return params;
}
public String getMessage() {
return String.valueOf(reason);
}
}
ServiceException:自定义业务层异常类
package com.demo.saas.basic;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 业务层异常类
*/
public class ServiceException extends BasicException {
private static final long serialVersionUID = 3371736236625831376L;
// 模块名
private String module;
// 错误信息
private String errorMessage;
private static Map<String, Properties> moduleProps = null;
/**
* 业务异常编码文件存放路径,@符号为模块名
*/
private final static String EXCEPTION_PATH = "com/demo/saas/@/serviceException.properties";
public ServiceException(Throwable cause) {
super(cause);
}
public ServiceException(Integer reason, String... params) {
super(reason,params);
}
/**
* @param module
* 模块名
* @param reason
* 异常编码
* @param params
* 参数1,参数2..
*/
public ServiceException(String module, int reason, String... params) {
this(module,reason, null, params);
}
public ServiceException(String module ,int reason, Throwable cause,String ... params) {
super(reason, params,cause);
this.module = module;
if(module != null){
initErrorMessage();
}
}
private void initErrorMessage() {
try {
Properties props = null;
if (!ServiceException.getModuleProps().containsKey(module)
|| ServiceException.getModuleProps().get(module).size() < 1) {
String path = EXCEPTION_PATH.replaceFirst("@", module);
props = loadProperties(path);
ServiceException.getModuleProps().put(module, props);
}
props = ServiceException.getModuleProps().get(module);
if (props == null) {
return;
}
String errInfo = props.getProperty(String.valueOf(getReason()));
if (errInfo == null) {
return;
}
Object[] paras = getParams();
errorMessage = getFormatedMessage(errInfo, paras);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Properties loadProperties(String filePath) {
Properties prop = new Properties();
InputStream in = null;
InputStreamReader isr = null;
try {
in = ServiceException.class.getClassLoader().getResourceAsStream(filePath);
if (in != null) {
isr = new InputStreamReader(in, "utf-8");
prop.load(isr);
}
} catch (Exception e) {
System.out.println("Fail to load properties:" + filePath);
} finally {
try {
if (null != isr)
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (null != in)
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return prop;
}
/**
* 生成格式化的信息 使用参数替换模板中的占位符
*
* @param templateMessage
* @param paras
* @return
*/
private static String getFormatedMessage(String templateMessage, Object... paras) {
if (templateMessage == null) {
return "";
}
// 使用传入的参数替换模板中的占位符
String formatedMsg = MessageFormat.format(templateMessage, paras);
// 将未处理的占位符替换,用于处理少传参数的情况
String regex = "\\{[0|[1-9]\\d*]\\}";
String replacement = "";
formatedMsg = formatedMsg.replaceAll(regex, replacement);
return formatedMsg;
}
@Override
public String getMessage() {
return errorMessage;
}
public static Map<String, Properties> getModuleProps() {
synchronized (ServiceException.class) {
if (moduleProps == null) {
moduleProps = new HashMap<String, Properties>();
}
}
return moduleProps;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
}
HelloService
package com.demo.saas.service;
import com.demo.saas.basic.ServiceException;
public interface HelloService {
void testException() throws ServiceException;
}
HelloServiceImpl
package com.demo.saas.service;
import com.demo.saas.basic.ProjectModule;
import com.demo.saas.basic.ServiceException;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HelloService {
public void testException() throws ServiceException {
throw new ServiceException(ProjectModule.BASIC, 49001, new String[]{});
}
}
二、controller层
CommonExceptionHandler
package com.demo.saas.globalexception;
import com.demo.saas.basic.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
@Slf4j
public class CommonExceptionHandler {
/**
* 拦截ServiceException类的异常
* @param e
* @return
*/
@ExceptionHandler(ServiceException.class)
@ResponseBody
public Map<String,Object> exceptionHandler(ServiceException e){
Map<String,Object> result = new HashMap<String,Object>();
result.put("respCode", e.getModule());
result.put("respMsg", e.getMessage());
return result;
}
}
HelloController
package com.demo.saas.web;
import com.demo.saas.basic.ServiceException;
import com.demo.saas.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/testException")
public String testException() throws ServiceException {
helloService.testException();
return "success";
}
}