【问题解决】本地方法部署环境不存在的问题(投机取巧方法)

文章目录

0.问

ps:急就直接看2.问题解决

一个方法,本地可以跑,但是放到部署环境就无法跑,报错方法不存在。

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: com.itextpdf.text.Image.scaleToFit(Lcom/itextpdf/text/Rectangle;)V

解决思路:

1.怀疑pom有深层的依赖冲突。

1、执行 mvn dependency:tree -Dverbose

2、在有依赖的包下面加上排除

xml 复制代码
<exclusions>
     <exclusion>
            <artifactId>com.itextpdf</artifactId>
            <groupId>itextpdf</groupId>
        </exclusion>
<exclusions>

3、重试,没用!谢特。应该不是依赖版本冲突。

排除类冲突,应该也不是方法不存在,如果真是方法不存在,那么构建就会出问题,但是jar包构建没有出问题,能正常启动。

4、下载部署环境的依赖jar包,检查类方法都存在,且版本一致。

2.问题解决

在线上环境打印类里面所有的方法,找一个可以用的

还真被我找到一个重载的方法

java 复制代码
    private static void log() {
        String className = "com.itextpdf.text.Image";
        String methodName = "scaleToFit";
        Class<?>[] parameterTypes = {com.itextpdf.text.Rectangle.class};

        try {
            Class<?> clazz = Class.forName(className);

            // Check version
            Package pkg = clazz.getPackage();
            if (pkg != null) {
                String implementationVersion = pkg.getImplementationVersion();
                String specificationVersion = pkg.getSpecificationVersion();
                log.info("[getPdfWatermarkImageLog] Implementation Version: {}", implementationVersion);
                log.info("[getPdfWatermarkImageLog] Specification Version: {}", specificationVersion);
            } else {
                log.info("[getPdfWatermarkImageLog] Package information is not available.");
            }

            // Check methods
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                log.info("[getPdfWatermarkImageLog] Method: {}, name:{}, parameterTypes:{}", method, method.getName(), method.getParameterTypes());
            }

            // Check if a specific method exists
            try {
                Method specificMethod = clazz.getDeclaredMethod(methodName, parameterTypes);
                log.info("[getPdfWatermarkImageLog] Method :{} (Rectangle) exists. :{}", methodName, specificMethod);
            } catch (NoSuchMethodException e) {
                log.info("[getPdfWatermarkImageLog] Method {} (Rectangle) does not exist.", methodName);
            }
        } catch (ClassNotFoundException e) {
            log.error("[getPdfWatermarkImageLog] Class not found: {}", className, e);
        }
    }

果然,方法不存在,但是有另一个可以替代的

日志1: [getPdfWatermarkImageLog] Method scaleToFit(Rectangle) does not exist.

日志2:[getPdfWatermarkImageLog] Method: public void com.itextpdf.text.Image.scaleToFit(float,float), name:scaleToFit, parameterTypes:[float, float]

偷鸡解决。

相关推荐
踩着两条虫7 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB7 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
budingxiaomoli7 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫7 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁7 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
即使再小的船也能远航7 小时前
【Python】安装
开发语言·python
学习中.........7 小时前
从扰动函数的变化,感受红黑树带来的性能提升
java
Irissgwe7 小时前
类与对象(三)
开发语言·c++·类和对象·友元
计算机安禾8 小时前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
雪度娃娃8 小时前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++