问题描述:
项目是基于SpringBoot的多模块项目,main方法在manage模块中,使用maven-assembly-plugin完成打包,打包之后将配置文件和项目中用到的jar包分离,通过java -Xms512m -Xmx1g -cp conf:lib/* com.xxx.xxx.App命令启动服务,但统一异常处理失效
尝试的方法修改basePackages参数,指定扫描范围吗,但没有生效@RestControllerAdvice(basePackages = {"com.amt"})将统一异常类放在基础模块中,但没有生效
Maven配置信息
org.apache.maven.plugins
maven-compiler-plugin
3.6.0
1.8
1.8
org.apache.maven.plugins
maven-jar-plugin
3.0.2
*.xml
*.yml
*.properties
**/config/**
**/lan/**
**/processes/**
true
lib/
com.amt.itms.App
org.apache.maven.plugins
maven-assembly-plugin
3.3.0
${project.artifactId}
false
make-dist
package
single
src/assembly/assembly.xml
统一异常处理类@Slf4j
@RestControllerAdvice()
public class GlobalExceptionHandle {
/**
* 服务器报错统一返回信息
*/
private static final String INTERNAL_SERVER_ERROR = "内部服务器错误";
/**
* 约束不匹配统一处理,包含断言工具类,参数校验工具类
*
* @param e
* @return
* @see com.amt.itms.util.Assert
* @see com.amt.itms.verify.ValidatorUtil
*/
@ExceptionHandler(value = {
ConstraintNotMatchException.class}
)
public ResponseData constraintNotMatchExceptionHandler(
ConstraintNotMatchException e) {
return new ResponseData(ResponseData.ERROR, e.getMessage());
}
/**
* SpringBoot参数缺失统一处理
*
* @param e
* @return
*/
@ExceptionHandler(value = {
MissingServletRequestParameterException.class}
)
public ResponseData missingServletRequestParameterExceptionHandler(
MissingServletRequestParameterException e) {
log.warn("method:[{}], message:[{}], time:[{}]",
"missingServletRequestParameterExceptionHandler", e.getMessage(),
System.currentTimeMillis());
ResponseData responseData = new ResponseData(ResponseData.ERROR,
e.getMessage());
return responseData;
}
/**
* 请求方式错误
*
* @return
*/
@ExceptionHandler(value = {
HttpRequestMethodNotSupportedException.class}
)
public ResponseData httpRequestMethodNotSupportedExceptionHandler() {
return new ResponseData(ResponseData.ERROR, "HTTP请求方法错误");
}
/**
* Manager服务统一异常处理
*
* @param e
* @return
*/
@ExceptionHandler(value = {
ManagerServerException.class}
)
public ResponseData managerServerExceptionExceptionHandler(
ManagerServerException e) {
ResponseData responseData;
if (StringUtils.hasLength(e.getMessage())) {
responseData = new ResponseData(ResponseData.ERROR, e.getMessage());
} else {
responseData = new ResponseData(ResponseData.ERROR,
INTERNAL_SERVER_ERROR);
}
return responseData;
}
@ExceptionHandler(value = {
Exception.class}
)
public ResponseData exceptionHandle(Exception e) {
ResponseData responseData;
if (e instanceof NoHandlerFoundException) {
responseData = new ResponseData(ResponseData.ERROR, "接口地址不存在");
} else {
responseData = new ResponseData(ResponseData.ERROR, e.getMessage());
}
log.error("method:{}; exception:{};time:{}", "exceptionHandle", e,
System.currentTimeMillis());
e.printStackTrace();
return responseData;
}
}
遇到这个问题有点抓狂了,在idea中都是生效的,打包为一个单独的jar包也是生效的,但分离打包之后就失效了,还请各位大佬帮帮忙!