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

文章目录

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]

偷鸡解决。

相关推荐
脑袋大大的几秒前
判断当前是否为钉钉环境
开发语言·前端·javascript·钉钉·企业应用开发
Wy. Lsy28 分钟前
Kotlin基础学习记录
开发语言·学习·kotlin
全栈凯哥1 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
M1A11 小时前
Java集合框架深度解析:LinkedList vs ArrayList 的对决
java·后端
Tanecious.1 小时前
C++--红黑树
开发语言·c++
Top`1 小时前
Java 泛型 (Generics)
java·开发语言·windows
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
如何使用Java WebSocket API实现客户端和服务器端的通信?
java·开发语言·websocket
Shartin2 小时前
Can201-Introduction to Networking: Application Layer应用层
服务器·开发语言·php
是小崔啊2 小时前
tomcat源码02 - 理解Tomcat架构设计
java·tomcat
没有bug.的程序员2 小时前
JAVA面试宝典 -《安全攻防:从 SQL 注入到 JWT 鉴权》
java·安全·面试