问题描述:
项目是基于SpringBoot的多模块项目,main方法在manage模块中,使用maven-assembly-plugin
完成打包,打包之后将配置文件和项目中用到的jar包分离,通过java -Xms512m -Xmx1g -cp conf:lib/* com.xxx.xxx.App
命令启动服务,但统一异常处理失效
尝试的方法
- 修改
basePackages
参数,指定扫描范围吗,但没有生效
@RestControllerAdvice(basePackages = {"com.amt"})
- 将统一异常类放在基础模块中,但没有生效
Maven配置信息
<build>
<plugins>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--Jar打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>*.xml</exclude>
<exclude>*.yml</exclude>
<exclude>*.properties</exclude>
<exclude>**/config/**</exclude>
<exclude>**/lan/**</exclude>
<exclude>**/processes/**</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--指定主类-->
<mainClass>com.amt.itms.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!--assembly自定义打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
统一异常处理类
@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包也是生效的,但分离打包之后就失效了,还请各位大佬帮帮忙!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…