Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.6k views
in Technique[技术] by (71.8m points)

Java多模块项目,分离打包后统一异常处理失效

问题描述:

项目是基于SpringBoot的多模块项目,main方法在manage模块中,使用maven-assembly-plugin完成打包,打包之后将配置文件和项目中用到的jar包分离,通过java -Xms512m -Xmx1g -cp conf:lib/* com.xxx.xxx.App命令启动服务,但统一异常处理失效

尝试的方法

  1. 修改basePackages参数,指定扫描范围吗,但没有生效
@RestControllerAdvice(basePackages = {"com.amt"})
  1. 将统一异常类放在基础模块中,但没有生效

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

无意间看到一篇文章,看到了解决此问题的方法。是我的项目中有两个统一异常处理类,删除其中一个即可


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...