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

文章目录

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]

偷鸡解决。

相关推荐
♡喜欢做梦5 小时前
MyBatis XML 配置文件:从配置规范到 CRUD 开发实践
xml·java·java-ee·mybatis
爱吃烤鸡翅的酸菜鱼5 小时前
Spring Boot 实现 WebSocket 实时通信:从原理到生产级实战
java·开发语言·spring boot·后端·websocket·spring
J不A秃V头A5 小时前
Maven的分发管理与依赖拉取
java·maven
雪域迷影5 小时前
C++中编写UT单元测试用例时如何mock非虚函数?
开发语言·c++·测试用例·gmock·cpp-stub开源项目
AI街潜水的八角6 小时前
Python电脑屏幕&摄像头录制软件(提供源代码)
开发语言·python
hadage2336 小时前
--- git 的一些使用 ---
开发语言·git·python
lly2024068 小时前
HTML与CSS:构建网页的基石
开发语言
一只会写代码的猫8 小时前
面向高性能计算与网络服务的C++微内核架构设计与多线程优化实践探索与经验分享
java·开发语言·jvm
萤丰信息9 小时前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧10 小时前
Eclipse为方法添加注释
java·ide·eclipse