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

文章目录

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]

偷鸡解决。

相关推荐
学长毕设6 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
C^h6 小时前
c++ string
开发语言·c++
raindayinrain7 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的7 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao7 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
zzu123zsw7 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
学逆向的8 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的8 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”8 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
源码集结号8 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统