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

文章目录

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]

偷鸡解决。

相关推荐
gaoliheng0062 分钟前
Redis看门狗机制
java·数据库·redis
我是唐青枫4 分钟前
.NET AOT 详解
java·服务器·.net
lexiangqicheng7 分钟前
JS-- for...in和for...of
开发语言·前端·javascript
我是老孙26 分钟前
windows10 php报错
开发语言·php
y1021210428 分钟前
Python训练营打卡Day42
开发语言·javascript·ecmascript
Su米苏28 分钟前
Axios请求超时重发机制
java
2301_8050545639 分钟前
Python训练营打卡Day46(2025.6.6)
开发语言·python
曹勖之1 小时前
撰写脚本,通过发布/joint_states话题改变机器人在Rviz中的关节角度
开发语言·python·机器人·ros2
不惑_1 小时前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json
梓仁沐白1 小时前
【Kotlin】注解&反射&扩展
开发语言·python·kotlin