java打包后监听器失灵_Java多模块项目,分离打包后统一异常处理失效

本文描述了一个基于SpringBoot的多模块项目在打包后,使用maven-assembly-plugin分离配置文件和jar包,导致统一异常处理不再生效的问题。尝试了修改basePackages参数、调整异常处理类的位置等方法未成功。分享了项目的Maven配置信息,并提供了全局异常处理类的代码。寻求社区帮助解决打包后异常处理失效的难题。
摘要由CSDN通过智能技术生成

问题描述:

项目是基于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包也是生效的,但分离打包之后就失效了,还请各位大佬帮帮忙!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值